我有以下两张表
Groups
Id (int)
People
Id (int)
GroupId (int, Groups.Id)
IsSelected (bit)
这将在单个查询中返回所有组及其所有成员(人员)
var grps = myDatabase.Groups.Include("People");
如何编写一个查询来返回所有已选择人员的组(IsSelected = true)?
我有以下两张表
Groups
Id (int)
People
Id (int)
GroupId (int, Groups.Id)
IsSelected (bit)
这将在单个查询中返回所有组及其所有成员(人员)
var grps = myDatabase.Groups.Include("People");
如何编写一个查询来返回所有已选择人员的组(IsSelected = true)?
让我知道这个是否奏效
var grps = myDatabase.Groups.Select(g=> new { g, people = g.People.Where(p=>p.IsSelected)});
您将需要使用“加入”方法,如下所示:
(from g in myDatabase.Groups
join p in myDatabase.People on g.Id equals p.GroupId
where p.IsSelected == true
select g);
这将为您提供选择了人员的所有组。
或查看 .Where()
就像是
var grps = myDatabase.Groups.Include("People").Where(x => x.IsSelected);
//x => !x.IsSelected for false