Reputation: 53
I want to filter the descendants of School by one expression. Is this possible? I want only the pupils with the Age of "12"
Pseudocode
context.Schools.Where(s => s.Name = "Springfield Elementary").Where(s =>
s.Pupils.Age == 12).Select(s);
Basically i want a Collection of Schools which has an Collection of pupils where match my expression
Thanks!
Upvotes: 2
Views: 67
Reputation: 5210
context.Schools.Where(s => s.Name == "Springfield Elementary" && s.Pupils.Age == 12);
It shouldn't be nessesary to .Select(s)
school since that's what you are querying.
This returns the Schools that have pupils age 12. But if you want to select the pupils instead you need to:
context.Schools
.Where(s => s.Name == "Springfield Elementary" && s.Pupils.Age == 12)
.Select(s=>s.Pupils);
Or as suggested by comments querying the pupils instead
context.Pupils.Where(s => s.Age == 12 && s.School.Name == "Springfield Elementary");
This assumes that Schools/Pupil looks something like this (code-first POCOs):
public class School
{
public int SchoolId { get; set; }
public string Name { get; set; }
public virtual ICollection<Pupil> Pupils { get; set; }
}
public class Pupil
{
public int PupilId { get; set; }
public int Age { get; set; }
public int SchoolId { get; set; }
public virtual School School { get; set; }
}
Upvotes: 1