Search code examples
sql-serverentity-frameworkforeign-keys

Entity Framework 6 search on foreign keyed table


We have a Customer Retuns database with two tables in it: "Returns" and "Cause". enter image description here

The Returns table has customer returns in it, while cause is a list of reasons for the return - A Return can have multiple Causes. What we are trying to do is a search on the Returns table along the lines of "Description.Contains('Table') and Returns.Cause.Contains('Wrong Colour')", Is there a neat way to perform this search or do I have retrieve "everything" from the tables and filter them out in multiple stages?


Solution

  • You can try Returns.Where(x -> x.Description.Contains("Table") && x.Cause.Any(z -> z.Name.Contains("Wrong colour"))