Reputation: 243
I need help to build a "query" using LINQ `where". I'm not getting this alone.
I need to create a conditional clause, but into a child list.
By the way, here is an example.
public void Main{
List<Father> Fathers = getFathers();
//How can I create this clause?
Fathers.Where(x=>x.age > 50 && x.ChildrenGirl.Where(y=>y.ID == 5))
}
public class Father{
public int ID { get; set; }
public int age {get; set;}
public List<ChildGirl> ChildrenGirl { get; set; }
public List<ChildBoy> ChildrenBoy { get; set; }
}
public class ChildGirl{
public int ID { get; set; }
public int power {get;set;}
}
public class ChildBoy{
public int ID { get; set; }
public int power {get;set;}
}
Upvotes: 0
Views: 1834
Reputation: 851
You can use Any
function to check if at least one exists with condition
Fathers.Where(x => x.age > 50 && x.ChildrenGirl.Any(y => y.ID == 5));
Upvotes: 2
Reputation: 7111
First make all your properties public. Nothing will work without that.
You may find using the query comprehension syntax is easier for queries like this. This is the query you are looking for, using that syntax:
var result = from father in GetFathers()
from girl in father.ChildrenGirl
where father.age > 50 && girl.ID == 5
select father;
When you are updating your question, you should provide an implementation of GetFathers()
that fills up a collection of Father
objects, each with some children. I'd have tested my code a lot more if you had done that.
Upvotes: 0