过滤LINQ实体包含查询

时间:2012-06-17 18:23:24

标签: c# linq entity-framework linq-to-sql

我有以下两个表

Groups
Id (int)

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

这将在一个查询中返回所有成员(人)的所有组

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

如何编写一个单独的查询,该查询将返回所有已选中人员的群组(IsSelected = true)?

3 个答案:

答案 0 :(得分:1)

让我知道这是否有效

    var grps = myDatabase.Groups.Select(g=> new { g, people = g.People.Where(p=>p.IsSelected)});

答案 1 :(得分:0)

您需要使用'join'方法,如下所示:

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

这将为您提供所有人选的人群。

答案 2 :(得分:-1)

或退房.Where()

这样的东西
var grps = myDatabase.Groups.Include("People").Where(x => x.IsSelected); 
//x => !x.IsSelected for false