[英]C# / LINQ / get all elements of sub-collection matching a condition?
我有一个 :
ObservableCollection<X> x_collection = new ObservableCollection();
public class X
{
public X()
{
Items = new ObservableCollection<Y>();
for(int i = 0; i < 10; i++)
{
Items.Add(new Y(i % 2 == 0));
}
}
public ObservableCollection<Y> Items {get; set;}
}
public class Y
{
public Y() : this(true) {}
public Y(bool y) { MyProperty = y; }
public bool MyProperty { get; set; }
}
我如何创建一个LINQ查询,该查询将返回IEnumerable或ObservableCollection,它们将仅获取具有MyProperty == true的Y元素? 我确实意识到这可能是一个非常简单的问题,但是我对LINQ atm感到很困惑。
如果可能的话,我想请求一个lambda查询-对我来说,它们更容易理解
var result = Items.Where( y => y.MyProperty );
var biggerResult = x_collection.SelectMany( x => x.Items.Where( y => y.MyProperty ) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.