0

I am very new to LINQ. I have run to a scenario where I need to find distinct from an object and remove the others. Please help. I have tried like this var xyz = referal.GroupBy(i => i.Select(x => x.identifier).Distinct().Select(g=>g)); but it is not returning the distinct values. Thanks in advance. Sample Object :

{
    "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": ""
            }
          }
        ]
      }
    ]

}

So I want to check the distinct identifier and take the first one irresoective of other values, something like this

{
        "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": ""
                }
              }
            ]
          }
        ]
}
Learner
  • 1
  • 1

1 Answers1

0

You need to implement the interface IEquatable<T> class "invitee", because Dinstinct use this to compare object. On this way i think you are comparing memory references.

Lucas Simas
  • 141
  • 1
  • 6