C# LINQ选择至少属于一个选定类别的公司

C# LINQ选择至少属于一个选定类别的公司,c#,linq,C#,Linq,我有一个类别列表(CategoryID的)列表CategoryID; 此列表包含一些基于用户先前选择的id 然后我有一个数据库,里面有可以成为一个或多个类别成员的公司。 这是在联接表CompaniesInCategory中维护的,它会生成类似company.Categories的对象结构 List<int> categoryIds = new List<int>() {123, 3, 5858, 23}; List<company> companies

我有一个类别列表(CategoryID的)列表CategoryID; 此列表包含一些基于用户先前选择的id

然后我有一个数据库,里面有可以成为一个或多个类别成员的公司。 这是在联接表CompaniesInCategory中维护的,它会生成类似company.Categories的对象结构

List<int> categoryIds = new List<int>() {123, 3, 5858, 23};    
List<company> companies = (from c in context.Companies
                           where c.Categories.(one of the Id's of these c.Categories should be in the list named categoryIds) 
                           select c);
现在我的问题是,我如何选择至少属于所选类别之一的所有公司

List<int> categoryIds = new List<int>() {123, 3, 5858, 23};    
List<company> companies = (from c in context.Companies
                           where c.Categories.(one of the Id's of these c.Categories should be in the list named categoryIds) 
                           select c);
List categoryIds=newlist(){123,35858,23};
上市公司=(上下文中来自c.companys
其中c.类别。(这些c.类别中的一个Id应位于名为CategoryID的列表中)
选择c);

每家公司都附有一份类别清单。从这个类别列表(c.categories)(都有一个CategoryId)中,至少有一个必须与列表CategoryId中的一个id相匹配。

我不像以前那样熟悉理解语法,但是假设category字段名为CategoryId,我相信lambda版本是:

List<int> categoryIds = new List<int>() {123, 3, 5858, 23};    
List<company> companies = (from c in context.Companies
                           where categoryIds.Contains(c.Categories)
                           select c).ToList();  //I would add .ToList() since List<company>
var companies = dc.Companies
    .Where(c => categoryIds.Contains(c.CategoryId))

由于我了解您的问题,我将检查(DB)类别表和类别列表。然后,从Category表中获取所有成员公司

List<int> categoryIds = new List<int>() {123, 3, 5858, 23};    
List<company> companies = (from c in context.Companies
                           from b in context.Categories //Asuming there is a index table of some sort
                           where categoryIds.Contains(b.CatID) && c.companyID == b.companyID
                           select c).ToList();
List categoryIds=newlist(){123,35858,23};
上市公司=(上下文中来自c.companys
在context.Categories//a中有一个某种索引表
其中categoryId.Contains(b.CatID)&&c.companyID==b.companyID
选择c.ToList();

“至少有一个”通常能最好地转化为LINQ的
Any()
方法。

似乎您正在谈论这一点:

var categoryIds=(new[] { 123, 3, 5858, 23 }).ToList();

var category=
    new {
        Id=123
    };

var company=
    new {
        Categories=(new[] { category }).ToList()
    };

var context=
    new {
        Companies=(new[] { company }).ToList()
    };

var companies=(
    from c in context.Companies
    from x in c.Categories
    from y in categoryIds
    where x.Id==y
    select c
    ).ToList();
因此,在您指定的地方:

where c.Categories.(one of the Id's of these c.Categories should be in the list named categoryIds)
将是:

where c.Categories.Any(x => categoryIds.Contains(x.Id))
因为
公司
相当于:

var companies=(
    from c in context.Companies
    where c.Categories.Any(x => categoryIds.Contains(x.Id))
    select c
    ).ToList();

也许
加入x的公司在x上的incategory.CompanyId等于c.CompanyId
我的答案是你想要的吗?它将从Companys表中选择categoryIds列表中包含类别的所有行()否,这不起作用,因为在categoryIds.Contains(c.Categories)中,categoryIds是一个列表,c.Categories是一个类别对象列表。问题是,我的company对象上没有一个CategoryId,而是一个类别列表。因此,必须匹配2个列表。如果任何公司类别ID与列表中的任何类别ID匹配,则where表达式返回true。根据您最初的查询,看起来您正在尝试获取公司列表。c.类别不是int的列表,而是类别对象的列表。否-“c”是公司列表,CategoryId是列表,此代码将获取一个列表,其中列表中的每个公司对象都有一个mathes ID在列表中,我弄错了吗?这似乎是你所问的问题。每个公司都有一个分类列表。从这个类别列表(c.categories)(都有CategoryId)中,至少有一个必须与列表CategoryId中的一个id匹配
var companies=(
    from c in context.Companies
    where c.Categories.Any(x => categoryIds.Contains(x.Id))
    select c
    ).ToList();