Linq 按复杂对象分组(对象的 E.G 列表元素)

Linq Group by Complex Object (E.G list element of object)

我有如下对象;

var json = @"{
  'TableNumber': '3',
  'TableId': '81872d39-9480-4d2d-abfc-b8e4f33a43b6',
  'tableMenuItems': [
    {
      'Id': '4664a2d3-c0af-443d-8af5-2bd21e71838b',
      'Name': 'Bonfile',
      'tableMenuItemCondiment': [
        {
          'Id': '9b1f01a0-0313-46b6-b7f1-003c0e846136',
          'Name': 'Kekikkli',
          'Price': 0
        },
        {
          'Id': '38a9cce6-f20c-4f78-b6c9-c15e79ecc8f1',
          'Name': 'mayonez',
          'Price': 0
        },
        {
          'Id': 'cb3a7811-668b-4e45-bce3-e6b2b13af9e1',
          'Name': 'Rare',
          'Price': 0
        }
      ],
      'Price': 45,
      'Count': 1
    },
    {
      'Id': '4664a2d3-c0af-443d-8af5-2bd21e71838b',
      'Name': 'Bonfile',
      'tableMenuItemCondiment': [
        {
          'Id': 'c3c62f7d-987d-4523-849c-932334db4e4f',
          'Name': 'Kekikkli',
          'Price': 0
        },
        {
          'Id': 'cbb16028-6e77-4e4b-a1c9-d969e804f093',
          'Name': 'mayonez',
          'Price': 0
        },
        {
          'Id': '16f9efc2-73d3-4a09-87fc-e1fb76957ed2',
          'Name': 'Rare',
          'Price': 0
        }
      ],
      'Price': 45,
      'Count': 1
    }
  ],
  'TotalPrice': 90
}";

正如您在上面看到的,该对象具有作为 tableMenuItems 的列表元素。 tableMenuItems 属性 中有两个对象,它们完全相同。我想像下面这样对这个对象进行分组;

var json = @"{
  'TableNumber': '3',
  'TableId': '81872d39-9480-4d2d-abfc-b8e4f33a43b6',
  'tableMenuItems': [
    {
      'Id': '4664a2d3-c0af-443d-8af5-2bd21e71838b',
      'Name': 'Bonfile',
      'tableMenuItemCondiment': [
        {
          'Id': '9b1f01a0-0313-46b6-b7f1-003c0e846136',
          'Name': 'Kekikkli',
          'Price': 0
        },
        {
          'Id': '38a9cce6-f20c-4f78-b6c9-c15e79ecc8f1',
          'Name': 'mayonez',
          'Price': 0
        },
        {
          'Id': 'cb3a7811-668b-4e45-bce3-e6b2b13af9e1',
          'Name': 'Rare',
          'Price': 0
        }
      ],
      'Price': 90,
      'Count': 2
    }    
  ],
  'TotalPrice': 90
}";

如您所见,如果两个 tablemenuitems 相同并且它们的调味品相同我将它们分组。我制作了一些 IEqualityComparer 用于 gruping,但它不起作用我除外。

这是我在下面看到的内容 link;

https://dotnetfiddle.net/QQFfIy

我不明白我做错了什么

1 ) 我创建了我的 类 以满足 json,

public class MyTable {
    public string TableNumber { get; set; }
    public string TableId { get; set; }
    public List<TableMenu> tableMenuItems { get; set; }
    public int TotalPrice { get; set; }
}

public class TableMenu {
    public string Id { get; set; }
    public string Name { get; set; }
    public List<TableMenuItemCondiment> tableMenuItemCondiment { get; set; }
    public int Price { get; set; }
    public int Count { get; set; }
}

public class TableMenuItemCondiment {
    public string Id { get; set; }
    public string Name { get; set; }
    public int Price { get; set; }
}

2 ) 然后我用 NewtonSoft 反序列化了对象,

MyTable myTable = JsonConvert.DeserializeObject<MyTable>(json);

3 ) 转换对象为,

MyTable result = new MyTable() {
                TableNumber = myTable.TableNumber,
                TableId = myTable.TableId,
                tableMenuItems = myTable.tableMenuItems.GroupBy(x => x.Id).Select(x => new TableMenu() {
                    Id = x.Key,
                    Name = x.First()?.Name,
                    tableMenuItemCondiment = x.SelectMany(a=>a.tableMenuItemCondiment).GroupBy(z => z.Name).Select(z => new TableMenuItemCondiment() {
                        Id = z.First()?.Id,
                        Name = z.Key,
                        Price = z.Sum(t => t.Price)
                    }).ToList(),
                    Price = x.Sum(t => t.Price),
                    Count = x.Sum(t => t.Count)
                }).ToList(),
                TotalPrice = myTable.TotalPrice
            };

4 ) 最后序列化对象,看看是否给出了你想要的响应,

string output = JsonConvert.SerializeObject(result);

5 ) 打印输出,

Console.WriteLine(output);
Console.ReadKey();

输出:

{  "TableNumber":"3", 
  "TableId":"81872d39-9480-4d2d-abfc-b8e4f33a43b6", 
  "tableMenuItems":[
     {
        "Id":"4664a2d3-c0af-443d-8af5-2bd21e71838b",
        "Name":"Bonfile",
        "tableMenuItemCondiment":[
           {
              "Id":"9b1f01a0-0313-46b6-b7f1-003c0e846136",
              "Name":"Kekikkli",
              "Price":0
           },
           {
              "Id":"38a9cce6-f20c-4f78-b6c9-c15e79ecc8f1",
              "Name":"mayonez",
              "Price":0
           },
           {
              "Id":"cb3a7811-668b-4e45-bce3-e6b2b13af9e1",
              "Name":"Rare",
              "Price":0
           }
        ],
        "Price":90,
        "Count":2
     }    
],    
"TotalPrice":90 }