开发者

C# / LINQ / get all elements of sub-collection matching a condition?

I have an :

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>开发者_StackOverflow中文版 Items {get; set;}
}

public class Y
{
    public Y() : this(true) {}
    public Y(bool y) { MyProperty = y; }
    public bool MyProperty { get; set; }
}

How do I create a LINQ query that will return an IEnumerable or ObservableCollection that will only get Y elements that have their MyProperty == true? I do realize it's probably a very easy question but I'm pretty confused with LINQ atm.

If possible I'd like to ask for a lambda-query - they're much easier for me to understand


var result = Items.Where( y => y.MyProperty );

var biggerResult = x_collection.SelectMany( x => x.Items.Where( y => y.MyProperty ) );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜