Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按复杂对象划分的Linq组(例如对象的列表元素)_C#_Linq_Gethashcode_Iequalitycomparer - Fatal编程技术网

C# 按复杂对象划分的Linq组(例如对象的列表元素)

C# 按复杂对象划分的Linq组(例如对象的列表元素),c#,linq,gethashcode,iequalitycomparer,C#,Linq,Gethashcode,Iequalitycomparer,我有一个像下面这样的物体 var json = @"{ 'TableNumber': '3', 'TableId': '81872d39-9480-4d2d-abfc-b8e4f33a43b6', 'tableMenuItems': [ { 'Id': '4664a2d3-c0af-443d-8af5-2bd21e71838b', 'Name': 'Bonfile', 'tableMenuItemCondiment': [

我有一个像下面这样的物体

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
}";
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。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': 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
}";
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
}";
正如你们所看到的,若两个餐具是一样的,调味品也是一样的,我就把它们分组。我做了一些质量比较,但这不是我所期望的工作

以下是我在下面的链接中看到的内容

我不明白我做错了什么

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

现在还不清楚你想要达到什么目标。这些小提琴会在适当的时候消失。把代码放在你的问题中。根本不清楚你想要实现什么。这些小提琴会在适当的时候消失。把代码放在你的问题中。因为假设所有属性都是对象的复合键是没有意义的,我提议根据你想要的输出假设对象的ID和名称是唯一的。如果所有项目都有相同数量的调味品,这个选项很好。但若两个相同Id和名称的菜单项有不同大小的调味品,那个么它就错了。但是,顺便说一句,我试图在iequalitycomparer及其工作的帮助下解决这个问题。如果你愿意的话,我可以顺便和你分享。无需共享,但谢谢:)由于假设所有属性都是对象的复合键没有意义,我提议根据您想要的输出,假设对象的ID和名称是唯一的。如果所有项都有相同数量的调味品,则此选项很好。但若两个相同Id和名称的菜单项有不同大小的调味品,那个么它就错了。但是,顺便说一句,我试图在iequalitycomparer及其工作的帮助下解决这个问题。如果你愿意的话,我可以顺便和你分享。无需分享,但谢谢:)