首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >以多对多关系获取数据

以多对多关系获取数据
EN

Stack Overflow用户
提问于 2013-11-22 01:21:28
回答 4查看 373关注 0票数 3

我有三个实体:

代码语言:javascript
复制
public class KeywordSearch
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }

    // Navigation properties
    public Keyword Keyword { get; set; }
}

public class Keyword
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }

    // Navigation properties
    public virtual ICollection<Address> Addresses { get; set; }
}

public class Address
{
    // Primary properties
    public int Id { get; set; }
    public PTCouncil PTCouncil { get; set; }                 <---------- EDIT

    // Navigation properties
    public virtual ICollection<Keyword> Keywords { get; set; }
}

public class PTCouncil                                       <---------- EDIT
{
    // Primary properties
    public int Id { get; set; }
    public string Name { get; set; }
}

根据一组单词,我需要提取所有不同的地址Id。

将在KeywordSearch表中搜索与地址相关的匹配关键字的单词。

到目前为止,在William的帮助下,我做到了这一点,但是获取匹配所有和部分单词的关键字,并且我需要将它们全部获取:

编辑:

代码语言:javascript
复制
var addressIds = (
              from ks in keywordSearchQuery
              where splitKeywords.Contains(ks.Name)
              select ks.Keyword.Addresses.Select(k => k.Id)
             )
             .ToList()
             .Aggregate((a, b) => a.Intersect(b));

示例:

代码语言:javascript
复制
KeywordSearch = {1,"RENAULT",1},{2,"MORAIS",2},{3,"SOARES",3},{4,"CENTRO",4}
Keyword       = {1,"Renault",{1,2}},{2,"Morais",{1}},{3,"Soares",{1}},{4,"Centro",{2}}
Address       = {1,"Renault Morais Soares",{1,2,3}},{2,"Renault Centro",{1,2}}

If I search "RENAULT MORAIS SOARES", I should get AddressId = 1
If I search "RENAULT CENTRO", I should get AddressId = 2
If I search "RENAULT", I should get AddressId = 1,2

Actual Search Problem: If I search "RENAULT XXXX", I get 1,2 and I should get nothing.

我还需要按位置过滤,我已经尝试过了,但我得到了一个错误“指定的类型成员'PTCouncil‘在LINQ to Entities中不受支持”

代码语言:javascript
复制
keywordsAddressIds = from ks in keywordSearchQuery
                     where splitKeywords.Contains(ks.Name)
                     select ks.Keyword.Addresses.Where(p => p.Location.Distance(centerPoint) < radius * 1000).Select(a => a.Id);

有什么想法吗?

谢谢。

EN

Stack Overflow用户

发布于 2013-12-05 13:14:59

我认为您应该尝试使用inner join,它也用于从许多表获取数据

Select TABLE1.Field1,TABLE1.Field2,TABLE2.Field1 from TABLE1 INNER JOIN TABLE2 on TABLE1.Field1=TABLE2.Field1

票数 0
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20127755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档