[英]Linq - Narrow down a list object of events by tags within
I'm a little bit stumped with this Linq statement. 我对Linq的声明感到有点难过。 My basic database structure is:
我的基本数据库结构是:
I have a list of events, and I want to narrow it down by events that either contain the specified tag, or the venue they are held at contain the tag. 我有一个事件列表,我希望通过包含指定标记的事件或它们所包含的标记包含标记的事件来缩小它。
I have tried this, and I think I may not be too far off, but I'm still struggling to get it to work: 我试过这个,我想我可能不会太离谱,但我仍然在努力让它发挥作用:
eventsList = eventsList
.SelectMany(x => x.EventTag)
.Where(et => et.TagID == tagID)
.Select(et => t.Event)
.Union(eventsList.SelectMany(x=>x.Venue.VenueTag)
.Where(vt =>vt.TagID == tagID)
.Select(vt=>vt.Venue.Event)
.Distinct()
.ToList();
I'd be very grateful for any help! 我会非常感谢任何帮助!
Andy 安迪
You can use Any
for this: 你可以使用
Any
:
eventsList.Where(
x => x.EventTag.Any(et => et.TagID == tagID)
|| x.Venue.Any(v => v.Any(vt => vt.TagID == tagID)))
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.