[英]Querying Child Collections in LINQ
I have a collection of objects called Gigs
. 我有一组名为
Gigs
的对象。
Each Gig
has an Acts
collection. 每个
Gig
都有一个Acts
系列。
Using Linq I want to query my collection of gigs to get all gigs where with an act that has an id of 7 for example. 使用Linq我想查询我的演出集合以获取所有演出,其中例如具有id为7的动作。
act.id = 7;
So I started writting... 所以我开始写作......
return from gig in qry
where gig.Acts //not sure how to do this bit
select gig;
But I'm not sure how you set conditions on the child collection called acts. 但是我不确定你是如何为名为actions的子集合设置条件的。
Any ideas? 有任何想法吗?
Essentially the same as Mike_G, only more verbose syntax and using equality. 基本上与Mike_G相同,只是更详细的语法和使用相等。
var myCollection = from gig in qry
where gig.Acts.Any(act => act.ID == 7)
select gig;
Just an edit to bring comments to the answer: 只是编辑带来评论答案:
Actually query is for an ID on a member (Artist) on the Act object that can be null. 实际上,查询是针对Act对象上的成员(Artist)上的ID,该ID可以为null。
new Query: 新查询:
var myCollection = from gig in qry
where gig.Acts.Any(act => (null != act.Artist) && (act.Artist.ID == 7))
select gig;
var x = gigs.Where(g=>g.Acts.Select(a=>a.ID).Contains(7));
these two queries also return the same: 这两个查询也返回相同:
var x = gigs.Where(g=>g.Acts.Count(a=>a.ID == 7) > 0);
var x = gigs.Where(g=>g.Acts.FirstOrDefault(a=>a.ID == 7) != null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.