Filtering a query in Entity Framework based upon a child value
开发者_StackOverflowI've got a model set up in the Entity Framework (EF) where there are two tables, parent and child, in a one to many relationship. Where I am having trouble is writing a query with linq where I am trying to retrieve a single instance of the parent while filtering upon a field in the parent and another field in the child. It would look something like what is listed below:
var query = from c in context.ParentTable
where c.IsActive == true && c.ChildTable.Name = "abc"
select c;
Unfortunately when I try this it fails because no field named "Name" appears available via Intellisense when I type c.ChildTable.
Any guidance would be appreciated.
That is correct because c.ChildTable is not of your Child type but EntityCollection<Child>. In order to make your query work, you need to modify it like this:
var query = from p in context.ParentTable
from c in p.ChildTable
where p.IsActive == true
&& c.Name == "abc"
select p;
精彩评论