C# LINQ-查找给定列表中的所有项都包含在icollection中的所有记录
我有一个过滤器列表,我想用它来查询一个表,其中返回的项包含所有值,而不仅仅是一个值 该表称为“组织”,包含该组织针对的年龄组列表C# LINQ-查找给定列表中的所有项都包含在icollection中的所有记录,c#,list,linq,collections,C#,List,Linq,Collections,我有一个过滤器列表,我想用它来查询一个表,其中返回的项包含所有值,而不仅仅是一个值 该表称为“组织”,包含该组织针对的年龄组列表 public class Organisation { public int OrganisationID { get; set; } public string OrgName { get; set; } public ICollection<OrgAgeGroup> AgeGroupCollection { get; set; } } 最后,从复选框
public class Organisation
{
public int OrganisationID { get; set; }
public string OrgName { get; set; }
public ICollection<OrgAgeGroup> AgeGroupCollection { get; set; }
}
最后,从复选框中选择的值存储在int列表中:
List<int> selectedAges
列出所选日期
我使用以下代码选择包含任何选定日期的所有组织:
IQueryable<Organisation> orglist = GetAllOrgs();
orglist = orglist.Where(o => o.AgeGroupCollection.Any(l => selectedAges.Contains(l.AgeGroupID)));
IQueryable orglist=GetAllOrgs();
orglist=orglist.Where(o=>o.AgeGroupCollection.Any(l=>selectedAges.Contains(l.AgeGroupID));
但我不知道如何获取包含所有选定年龄的所有Org。将Any改为All似乎对搜索没有影响。有什么想法吗?下面的查询应该会给出您想要的
orglist = orglist.Where(
o => selectedAges.All(
s => o.AgeGroupCollection
.Select(l => l.AgeGroupID)
.Contains(s)));
您需要组织o
,其中所有选定的日期s
都包含在o
的AgeGroupCollection
的AgeGroupId
集合中
orglist = orglist.Where(
o => selectedAges.All(
s => o.AgeGroupCollection
.Select(l => l.AgeGroupID)
.Contains(s)));