如何在字典列表中排序项目

How to order items in a list of dictionaries

我想 return children 和 grandchildren 记录(即 itemNo)按照它们更新到数据库的顺序。下面的查询按 itemNo ASC(默认)对记录进行排序。我相信按时间戳排序,return 记录的顺序与更新顺序相同。

我尝试如下添加时间戳,但没有按预期工作。

查询 return children 和孙子:

public async Task<List<Child>> GetChild(string parentItemNo)
{
           return await DbContext.Items
                .Where(x => x.ItemNo== parentItemNo)
                .SelectMany(x => x.Children.OrderByDescending(t => t.Timestamp).Select(c => new Child
                    {
                        ItemNo = c.ItemNo,
                        ItemName = c.ItemName,  
                        Timestamp = c.Timestamp,
                        GrandChildItemNos = c.Children.OrderByDescending(t => t.Timestamp).Select(gc => 
                           new GrandChild 
                           { 
                              ItemNo = gc.ItemNo, 
                              ItemName = gc.ItemName,
                              Timestamp = gc.Timestamp,
                           })  
                    })).ToListAsync();       // may i know how iadd orderBy here
 }

下面是我的 responseData 的样子:

[
    {"itemNo":"1111","itemName":"A1111", "timestamp":"AAAAAoc5I/o=", "grandChildSerialNos":[]}, 
    {"itemNo":"2222","itemName":"B2222"," "timestamp":"AAAAAoc5I/s=", "grandChildSerialNos":[]},
    {"itemNo":"3333","itemName":"C3333", "timestamp":"AAAAAoc5I/0=", "grandChildSerialNos": 
         [
             {"itemNo":"1234","itemName":"CH1234"..}, 
             {"itemNo":"5678","itemName":"CH5678"...}
         ]
    },      
    {"itemNo":"4444","itemName":"D4444", "timestamp":"AAAAAoc5I/4=" "grandChildSerialNos":[]}
   {"itemNo":"5555","itemName":"E5555", "timestamp":"AAAAAoc5I/w=" "grandChildSerialNos":[]}
]

我希望 responseData 到 return ItemNo“5555”在“3333”和“4444”之前(因为“5555”在 3333 和 4444 之前更新)。但响应不是 return 按此顺序编辑的。我希望 'ItemNo' 按 'Timestamp' 而不是升序排序。

型号class如下:

public partial class Item
{
    public byte[] Timestamp { get; set; }
    public string itemNo { get; set; }  
    public string itemName { get; set; } 
}

public class GrandChild
{
    public string ItemNo {get;set;}
    public string ItemName {get;set;}
    public byte[] Timestamp { get; set; }
}

public class Child
{
    public string ItemNo {get;set;}
    public string ItemName {get;set;}   
    public byte[] Timestamp { get; set; }
    public IEnumerable<GrandChild> GrandChildItemNos {get;set;}
}

你试过这样的东西吗?

await DbContext.Items.Where(x => x.ItemNo == parentItemNo)
                .SelectMany(x => x.Children)
                .OrderByDescending(t => t.Timestamp).Select(c => new Child
                {
                    ItemNo = c.ItemNo,
                    ItemName = c.ItemName,
                    Timestamp = c.Timestamp,
                    GrandChildItemNos = c.GrandChildItemNos.OrderByDescending(t => t.Timestamp).Select(gc =>
                          new GrandChild
                          {
                              ItemNo = gc.ItemNo,
                              ItemName = gc.ItemName,
                              Timestamp = gc.Timestamp,
                          })
                }).ToListAsync();