score:14

Accepted answer

If all items must be true:

var list = mainList.Where(x => x.detailList.All(y => y.property));

if atleast one

var list = mainList.Where(x => x.detailList.Any(y => y.property));

score:2

Try this, any will return True if "any" items in a list meet the condition.

var list = mainList.Where(x => x.detailList.Any(y => y.property == true));

score:1

Been answered, but here's a working example you can run in Linqpad:

void Main()
{
    var foo_list = new List<Foo>();
    foo_list.Add(new Foo(){InnerList = new List<Bar> {
        new Bar{ Pro=true,  Ind =1 },
        new Bar{ Pro=true,  Ind =2 },
        new Bar{ Pro=false, Ind =3 }
    }});
    foo_list.Add(new Foo(){InnerList = new List<Bar> {
        new Bar{ Pro=false, Ind =4 },
        new Bar{ Pro=false, Ind =5 },
        new Bar{ Pro=false, Ind =6 }
    }});

    var l = from x in foo_list
            where x.InnerList.Any(b => b.Pro)
            select x;
    l.Dump();
}

// Define other methods and classes here
class Foo{
    public List<Bar> InnerList {get;set;}
    public Foo() {
        InnerList = new List<Bar>();
    }
}

class Bar {
    public bool Pro { get; set; }
    public int Ind { get; set; }
}

The result will hold the first Foo, since some of its Bar's have a property Pro which is true

enter image description here


Related Articles