0

我有以下两张表

Groups
Id (int)

People
Id (int)
GroupId (int, Groups.Id)
IsSelected (bit)

这将在单个查询中返回所有组及其所有成员(人员)

var grps = myDatabase.Groups.Include("People");

如何编写一个查询来返回所有已选择人员的组(IsSelected = true)?

4

3 回答 3

1

让我知道这个是否奏效

    var grps = myDatabase.Groups.Select(g=> new { g, people = g.People.Where(p=>p.IsSelected)});
于 2012-06-17T20:35:59.593 回答
0

您将需要使用“加入”方法,如下所示:

(from g in myDatabase.Groups
 join p in myDatabase.People on g.Id equals p.GroupId
 where p.IsSelected == true
 select g);

这将为您提供选择了人员的所有组。

于 2012-06-17T18:31:34.470 回答
-1

或查看 .Where()

就像是

var grps = myDatabase.Groups.Include("People").Where(x => x.IsSelected); 
//x => !x.IsSelected for false
于 2012-06-17T18:41:23.537 回答