[英]EntityFramework sorting related entity
I have the following query which works fine 我有以下查询工作正常
var x = cc.Products
.Include("Category")
.Include("Supplier")
.Include("Manufacturer")
.Include("ProductPrices")
.Include("ProductPrices.Valuta")
.Include("ProductPrices.UnitOfMeasure")
.OrderBy(p => p.PartNumber);
I'm not trying to sort the ProductPrices descending so I can select the latest price. 我不尝试按降序排列ProductPrices,因此可以选择最新的价格。 However I can't get it into a single query.
但是我无法将其放入单个查询中。 Essentially I only need to select one of the ProductPrices based on the Id of that column.
本质上,我只需要根据该列的ID选择一个ProductPrices。
Is there any way to do this? 有什么办法吗? Or should I first evaluate the query, then ForEach them and Sort the navigation property?
还是我应该首先评估查询,然后对它们进行ForEach并对导航属性进行排序?
You can use a delegate method in your orderby clause: 您可以在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.