我不太理解为什么LinqKit中的select嵌套调用不起作用,希望有人能帮助我理解。
我的问题是:
首先让我列出有效的部分。
假设我们有三个数据库对象:
public class Customer {
public int Id {get; set;}
public string Name {get; set;}
public ICollection<Address> Addresses {get;set;}
}
public class Address {
public int Id {get;set;}
public string AddressLine {get;set;}
public int? Customer_Id { get; set; }
[ForeignKey("Customer_Id")]
public virtual Customer Customer {get; set;}
public int? Coordinates_Id { get; set; }
[ForeignKey("Coordinates_Id")]
public virtual Coordinates Coordinates {get; set;}
}
public class Coordinates {
public int Id {get;set;}
public double Latitude {get;set;}
public double Longitude {get;set;}
}
我有三个模型
public class CustomerModel {
public int Id {get; set;}
public string Name {get;set;}
public AddressModel Address {get;set;}
}
public class AddressModel{
public int Id {get;set;}
public string AddressLine {get;set;}
public CoordinatesModel Coordinates {get;set;}
}
public class CoordinatesModel {
public int Id {get;set;}
public double Latitude {get;set;}
public double Longitude {get;set;}
}
现在,我想要创建可重用的选择表达式。因此,我创建了这些内容:
public static Expression<Func<Address, AddressModel>> ToAddressModel = address =>
new AddressModel {
Id = address.Id,
AddressLine = address.AddressLine,
Coordinates = new Coordinates {
Id = address.Coordinates.Id,
Latitude = address.Coordinates.Latitude,
Longitude = address.Coordinates.Longitude
}
};
public static Expression<Func<Customer, CustomerModel>> ToCustomerModel = customer =>
new CustomerModel {
Id = customer.Id,
Name = customer.Name
Address = customer.Addresses.AsQueryable().Select(ToAddressModel).ToList()
};
最后,如果我要查询这个内容,我会写
dbContext.Customers
.AsExpandable()
.Select(ToCustomerModel)
.ToList();
这个代码可以如预期地运行。但是我希望现在能够使CoordinatesModel变得更加可复用,并创建一个表达式来映射它,并在AddressModels表达式中调用它。
public static Expression<Func<Coordinates, CoordinatesModel>> ToCoordinates = coordinates =>
new Coordinates {
Id = coordinates.Id,
Latitude = coordinates.Latitude,
Longitude = coordinates.Longitude
}
public static Expression<Func<Address, AddressModel>> ToAddressModel = address =>
new AddressModel {
Id = address.Id,
AddressLine = address.AddressLine,
Coordinates = ToCoordinatesModel.Invoke(address.Coordinates)
};
现在,如果我查询
dbContext.Customers
.AsExpandable()
.Select(ToCustomerModel)
.ToList();
它抛出异常
System.NotSupportedException: LINQ to Entities does not recognize the method 'CoordinatesModel Invoke[Coordinates,Model](System.Linq.Expressions.Expression`1[System.Func`2[Coordinates,CoordinatesModel]], Coordinates)' method
如果这不正确,那没关系,我只是想了解为什么不正确。如果我没有针对集合(如不确定的导航属性)进行调用,嵌套调用就没问题。