[英]LINQ Filter anonymous type based on IEnumerable values within type
I'm using LINQ to SQL like: 我正在使用LINQ to SQL,如:
var b =
from s in context.data
select new
{
id = s.id,
name = s.name
myEnumerable = s.OneToMany
};
Where myEnumerable is of type IEnumberable<T>
and I want to now get a subset of b
based upon properties of the individual items of myEnumerable
. 其中myEnumerable类型为
IEnumberable<T>
,我现在想根据myEnumerable
各个项目的属性获取b
的子集。 For example, say <T>
has properties Berry
and BerryID
, I would want to do something like: 例如,说
<T>
具有属性Berry
和BerryID
,我想这样做:
b =
from p in b
where //p.myEnumerable.myType.BerryID== 13
select p;
I'm feel like I'm missing something easy... 我觉得我想念一些简单的事情...
Are you looking to select p
if any of the items in p.myEnumerable
have BerryID
equal to 13? 如果
p.myEnumerable
任何项目的BerryID
等于13,您是否要选择p
?
b = from p in b
where p.myEnumerable.Any(t => t.BerryID == 13)
select p;
Or are you looking to select p
if all of the items in p.myEnumerable
have BerryID
equal to 13? 或者,如果
p.myEnumerable
所有项目的BerryID
等于13,您是否正在选择p
?
b = from p in b
where p.myEnumerable.All(t => t.BerryID == 13)
select p;
What exactly is the condition you want the items in p.myEnumerable
to fulfill before you select p
? 您希望在选择
p
之前要满足p.myEnumerable
的项目的条件是什么?
Keep only items with at least one item having BerryID
equal to 13 in the collection. 在集合中仅保留至少一项具有
BerryID
等于13的项目。
var b = context.data
.Where(s => s.OneToMany.Any(i => i.BerryID == 13))
.Select(s => new { id = s.id, name = s.name, myEnumerable = s.OneToMany });
Keep only items with all item having BerryID
equal to 13 in the collection. 在集合中仅保留所有
BerryID
等于13的项目。
var b = context.data
.Where(s => s.OneToMany.All(i => i.BerryID == 13))
.Select(s => new { id = s.id, name = s.name, myEnumerable = s.OneToMany });
Since myEnumerable is an IEnumerable you will have to do a where on that. 由于myEnumerable是IEnumerable,因此您必须在该位置执行操作。
var filteredData = from p in listOfData
where p.InnerData.Where(b=>b.ID == 13).Count() > 0
select p;
If I understand what you are saying...this is if there is an ID = 13 in the Enumerable at all. 如果我明白您的意思...这就是说Enumerable中是否有ID = 13。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.