Reputation: 4958
Hi I have a linq query which I use to get data from the DB.
And I have join two tables in the query.
Here is my database structure..
I need to get Customer with primary telephone number and default shipping address. And this is my query..
var customer=UnitOfWork.DbContext.Set<Domain.BoundedContext.ScreenPop.Entities.Customer>()
.Include(x => x.CustomerPhoneNumbers.Select(p => p.IsPrimary == true))
.Include(x => x.ShippingAddresses.Select(s => s.IsDefault == true))
.Where(c => c.CustomerId == customerQuery.CustomerId).FirstOrDefault();
But it gives me this error..
The Include path expression must refer to a navigation property defined on the type.
Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.
How can I get these information by using those three tables
Upvotes: 0
Views: 76
Reputation: 2786
The includes are just a way to say what navigation properties/tables you want included. Try this
var customer=UnitOfWork.DbContext.Set<Domain.BoundedContext.ScreenPop.Entities.Customer>()
.Include(x => x.CustomerPhoneNumbers)
.Include(x => x.ShippingAddresses)
.Where(c => c.CustomerId == customerQuery.CustomerId).FirstOrDefault();
and then just get the phone number and address you want
var phonenumber = customer.CustomerPhoneNumbers.FirstOrDefault(x=>x.IsPrimary);
var address = customer.ShippingAddresses.FirstOrDefault(x=>x.IsDefault);
Upvotes: 1