In my Customer
model, I've got
public virtual ICollection<AddressModel> AddressIDs { get; set; }
which references AddressModel
to give me a one-to-many relationship between customers and their addresses.
I've got a search function which uses
var CustomerList = _context.Customers
.Where(ps => ps.Surname.Contains(surnameToSearchFor))
to limit the returned dataset by surname.
I'm trying to add the ability to search for postcodes in the address. Following various links, this works in Visual Studio but breaks on execution
CustomerList = CustomerList
.Include(ps => ps.AddressIDs
.Where(a => a.Postcode == postcodeToSearchFor));
with the error
InvalidOperationException: The property expression 'ps => {from AddressModel a in ps.AddressIDs where ([a].Postcode == __p_0) select [a]}' is not valid. The expression should represent a property access: 't => t.MyProperty
How do I add a Where
clause to my LINQ on the sub table?
Edit To whoever suggested Multiple WHERE clause in Linq as the answer, that question clearly is relating to a single table, whereas I clearly asked about sub tables.