[英]How do I correctly use .OrderBy() in this scenario
我有界面
public interface IStoreItem
{
decimal Price { get; }
}
以及實現它的類。
public class ProductShell : IStoreItem
{
EFDbContext repository = new EFDbContext();
public int ShellID { get; set; }
public bool? Published { get; set; }
public decimal Price { get { return repository.ShellMembers.Where(x => x.ShellID == ShellID).Select(x => x.Product.ListPrice).Sum(); } }
}
價格設置正確,但是當我嘗試通過升序(默認)來執行OrderBy時,我看不到列表中的任何更改,並且物料仍保持舊位置。 作為設置了Price屬性的ProductShell對象,仍然按從高到低的順序排列,反之亦然。
List<IStoreItem> StoreItems = new List<IStoreItem>();
StoreItems.AddRange(repository.ProductShells.Where(x => x.Published != null));
StoreItems.OrderBy(x => x.Price);
.OrderBy
創建一個新序列。 您應該在此處使用List.Sort
。 或在添加到列表之前對其進行排序:
List<IStoreItem> StoreItems = repository.ProductShells
.Where(x => x.Published != null)
.OrderBy(x => x.Price)
.ToList<IStoreItem>();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.