代码之家  ›  专栏  ›  技术社区  ›  Hana Wujira

如何对对象中的对象值列表进行排序

  •  0
  • Hana Wujira  · 技术社区  · 6 年前

    这是我用来获取具有特定id的每个项目的项目和项目详细信息的查询

    public class ItemModel 
    {
       public int ItemSeq{get;set}
       public string ItemName{get;set;}
       public double? Amount{get;set;}
    }
    public class SampleModel
    {
        public string Id{ get; set; }
        public string Code{ get; set; }
        public List<ItemModel> Items{ get; set; }
    }
    
    public List<SampleModel> GetItems(string id)
    {
    
        var items= _context.ItemTable.Where(t => t.Id== id).ToList<SampleModel>();
        return list;
    }
    

    为了明确要求,有两个表 ItemTable ItemDetailTable 以及 Id 存在 项目表 中的主键和外键 ItamDetailTable ItemDetailTable表 可能有多个值 ID 上面的查询返回 对于特定的id,我想要的是按照 Amount . 有什么办法吗?

    样本数据

    Id     Code
    1      Code1
    2      Code3
    

    ItemDetailTable表

    Id    ItemSeq    ItemName    Amount
    1     1          ABC1        200
    1     2          ABC2        129
    1     3          ABC3        549
    2     1          DEF1        265
    2     2          DEf2        970
    

    我想要的是 返回值为1

    Id:   1
    Code: Code 1
    Items:[
        0:{
         ItemSeq:  2
         ItemName: ABC2
         Amount:   129
        }
       1:{
         ItemSeq:  1
         ItemName: ABC1
         Amount:   200
        },
       1:{
         ItemSeq:  3
         ItemName: ABC3
         Amount:   549
        }
    ] 
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   er-sho    6 年前

    您需要返回已排序的 ItemModel id 参数到 GetItems 方法。

    public List<ItemModel> GetItems(string id)
    {
        var items = _context.SampleTable.Where(t => t.Id == id).SelectMany(x => x.Items).Where(p => p.Amount != null).OrderBy(x => x.Amount).ToList();
        return items.ToList();
    }
    

    编辑:

    上面的查询返回ItemDetailTable中具有特定id的项和详细信息

    public class ItemDetailTable
    {
        public int ItemSeq { get; set; }
        public string ItemName { get; set; }
        public double? Amount { get; set; }
    
        public string Id { get; set; }   //Foreign key of ItemTable
    }
    
    
    public ItemTable GetItems(string id)
    {
        //Get "ItemTable" record from database
        var item = _context.ItemTable.Where(x => x.Id == id).SingleOrDefault();
    
        //Retrieve its "Items" and sort by ascending order
        var itemDetails = item.Items.Where(x => x.Id == id).Where(p => p.Amount != null).OrderBy(x => x.Amount).ToList();
    
        //Preare a new "ItemTable" object to return
        ItemTable itemTable = new ItemTable
        {
            Code = item.Code,
            Id = item.Id,
            Items = itemDetails
        };
    
        //Return new "itemTable" with sorted list of "ItemDetailTable"
        return itemTable;
    }
    

    编辑2:

    public ItemTable GetItems(string id)
    {
        var result = (from i in _context.ItemTable
                      where i.Id == id
                      let sorting = i.Items.Where(x => x.Id == id).Where(x => x.Amount != null).OrderBy(x => x.Amount).ToList()
                      select new ItemTable
                      {
                          Id = i.Id,
                          Code = i.Code,
                          Items = sorting
                      }).FirstOrDefault();   //Or => SingleOrDefault
    
        return result;
    }