简体   繁体   中英

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. 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.

Is there any way to do this? Or should I first evaluate the query, then ForEach them and Sort the navigation property?

You can use a delegate method in your orderby clause:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM