【问题标题】:Finding distinct and removing duplicates from object [duplicate]从对象中查找不同并删除重复项[重复]
【发布时间】:2020-02-26 09:53:20
【问题描述】:

我对 LINQ 很陌生。我遇到了一个场景,我需要从一个对象中找到不同的对象并删除其他对象。请帮忙。 我试过这样 var xyz = referal.GroupBy(i => i.Select(x => x.identifier).Distinct().Select(g=>g)); 但它没有返回不同的值。提前致谢。 示例对象:

{
    "referral_type": [
      {
        "type": "MOBILE",
        "invitee": [
          {
            "identifier": "12345678",
            "name": "Test2",
            "invited_on": "2020-01-29 12:25:46.0",
            "till": {
              "code": "",
              "name": ""
            }
          },
          {
            "identifier": "98765432",
            "name": "Test1",
            "invited_on": "2020-01-29 13:37:36.0",
            "till": {
              "code": "",
              "name": ""
            }
          },
          {
            "identifier": "12345678",
            "name": "Harry Test",
            "invited_on": "2020-01-29 13:55:32.0",
            "till": {
              "code": "",
              "name": ""
            }
          },
          {
            "identifier": "98765432",
            "name": "Harry Test",
            "invited_on": "2020-01-29 13:55:32.0",
            "till": {
              "code": "",
              "name": ""
            }
          }
        ]
      }
    ]

}

所以我想检查不同的标识符并取第一个不考虑其他值的标识符,像这样

{
        "referral_type": [
          {
            "type": "MOBILE",
            "invitee": [
              {
                "identifier": "12345678",
                "name": "Test2",
                "invited_on": "2020-01-29 12:25:46.0",
                "till": {
                  "code": "",
                  "name": ""
                }
              },
              {
                "identifier": "98765432",
                "name": "Test1",
                "invited_on": "2020-01-29 13:37:36.0",
                "till": {
                  "code": "",
                  "name": ""
                }
              }
            ]
          }
        ]
}

【问题讨论】:

  • 是否要从 json 中删除重复项?

标签: c# asp.net linq .net-core


【解决方案1】:

您需要实现接口IEquatable<T> class "invitee",因为Dinstinct 使用它来比较对象。这样,我认为您正在比较内存引用。

【讨论】:

    猜你喜欢
    • 2021-12-24
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多