争论异常被抓住了

时间:2014-06-05 09:05:47

标签: asp.net-mvc linq entity-framework asp.net-mvc-4

您好我有一个linq查询,用于从数据库中获取数据。

我在查询中加入了两个表。

这是我的数据库结构.. DB Structure

我需要向客户提供主要电话号码和默认送货地址。 这是我的疑问..

    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();

但它给了我这个错误..

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.

如何使用这三个表

获取这些信息

1 个答案:

答案 0 :(得分:1)

包含只是一种说明您想要包含哪些导航属性/表的方法。试试这个

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();

然后只需获取您想要的电话号码和地址

var phonenumber = customer.CustomerPhoneNumbers.FirstOrDefault(x=>x.IsPrimary);
var address = customer.ShippingAddresses.FirstOrDefault(x=>x.IsDefault);