繁体   English   中英

LINQ过滤基于类型内IEnumerable值的匿名类型

[英]LINQ Filter anonymous type based on IEnumerable values within type

我正在使用LINQ to SQL,如:

var b =  
   from s in context.data  
   select new  
   {   
     id = s.id,  
     name = s.name  
     myEnumerable = s.OneToMany
   };

其中myEnumerable类型为IEnumberable<T> ,我现在想根据myEnumerable各个项目的属性获取b的子集。 例如,说<T>具有属性BerryBerryID ,我想这样做:

b = 
   from p in b
   where //p.myEnumerable.myType.BerryID== 13
   select p;

我觉得我想念一些简单的事情...

如果p.myEnumerable任何项目的BerryID等于13,您是否要选择p

b = from p in b
    where p.myEnumerable.Any(t => t.BerryID == 13)
    select p;

或者,如果p.myEnumerable 所有项目的BerryID等于13,您是否正在选择p

b = from p in b
    where p.myEnumerable.All(t => t.BerryID == 13)
    select p;

您希望在选择p之前要满足p.myEnumerable的项目的条件是什么?

在集合中仅保留至少一项具有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 });

在集合中仅保留所有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 });

由于myEnumerable是IEnumerable,因此您必须在该位置执行操作。

var filteredData = from p in listOfData
                               where p.InnerData.Where(b=>b.ID == 13).Count() > 0
                               select p;

如果我明白您的意思...这就是说Enumerable中是否有ID = 13。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM