Ray Orellana
Ray Orellana

Reputation: 177

c# linq select list of objects looking inside another list of objects

i have a list of objects(1) with a list of another objects(2) inside. i want to select some objects(1) from the first list with some king of a .where looking in the other list(2).

my classes are something like that:

class objects(1)
{
    public int Id { set; get;}
    List<Objects(2)> Objects(2) { set; get;}
}
class objects(2)
{
    public int Id { set; get;}
    DateTime? Date { set; get;}
}

so i want to select all Objects(1) where List Objects(2) { set; get;} all the values in the list has DateTime? Date != null.

Upvotes: 1

Views: 1917

Answers (1)

mm8
mm8

Reputation: 169150

Try this:

List<Object1> listOfObjects1 = ...;
List<Object1> items = listOfObjects1.Where(x => x.Objects2 != null && !x.Objects2.Any(y => !y.Date.HasValue)).ToList();

Upvotes: 1

Related Questions