[英]EntityFramework sorting related entity
我有以下查询工作正常
var x = cc.Products
.Include("Category")
.Include("Supplier")
.Include("Manufacturer")
.Include("ProductPrices")
.Include("ProductPrices.Valuta")
.Include("ProductPrices.UnitOfMeasure")
.OrderBy(p => p.PartNumber);
我不尝试按降序排列ProductPrices,因此可以选择最新的价格。 但是我无法将其放入单个查询中。 本质上,我只需要根据该列的ID选择一个ProductPrices。
有什么办法吗? 还是我应该首先评估查询,然后对它们进行ForEach并对导航属性进行排序?
您可以在orderby子句中使用委托方法:
var x = cc.Products
.Include("Category")
.Include("Supplier")
.Include("Manufacturer")
.Include("ProductPrices")
.Include("ProductPrices.Valuta")
.Include("ProductPrices.UnitOfMeasure")
.OrderBy(delegate (Product p) {
//some logic here
//order by the greatest price
// e.g:
return p.ProductPrices.Max(i => i.Price);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.