簡體   English   中英

EntityFramework排序相關實體

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM