Home > Blockchain >  C# - Remove duplicate objects using LINQ
C# - Remove duplicate objects using LINQ

Time:06-02

I have below model classes

public class Hotel
{
    public string hoteltype { get; set; }
    public List<Room> rooms { get; set; }
}

public class Room
{
    public Room2 room { get; set; }
    public string name { get; set; }
}

public class Room2
{
    public int number { get; set; }
    public string category { get; set; }
    public string subCategory { get; set; }
}

public class Root
{
   public List<Hotel> hotels { get; set; }
}

Below is the JSON

 {
  "hotels": [
    {
      "hoteltype": "5star",
      "rooms": [
        {
          "room": {
            "number": 1,
            "category": "Deluxe",
            "subCategory": "type1"
          },
          "name": "xyz"
        },
        {
          "room": {
            "number": 2,
            "category": "Deluxe",
            "subCategory": "type2"
          },
          "name": "abc"
        }
      ]
    },
    {
      "hoteltype": "4star",
      "rooms": [
        {
          "room": {
            "number": 1,
            "category": "Small",
            "subCategory": "type1"
          },
          "name": "xyz"
        },
        {
          "room": {
            "number": 2,
            "category": "Small",
            "subCategory": "type4"
          },
          "name": "abc"
        }
      ]
    },
    {
      "hoteltype": "5star",
      "rooms": [
        {
          "room": {
            "number": 1,
            "category": "Deluxe",
            "subcategory": "type4"
          },
          "name": "xyz"
        },
        {
          "room": {
            "number": 2,
            "category": "Deluxe",
            "subcategory": "type5"
          },
          "name": "abc"
        }
      ]
    }
  ]
}


Expected Output:

{
  "hotels": [
    {
      "hoteltype": "5star",
      "rooms": [
        {
          "room": {
            "number": 1,
            "category": "Deluxe",
            "subCategory": null
          },
          "name": "xyz"
        },
        {
          "room": {
            "number": 2,
            "category": "Deluxe",
            "subCategory": null
          },
          "name": "abc"
        }
      ]
    },
    {
      "hoteltype": "4star",
      "rooms": [
        {
          "room": {
            "number": 1,
            "category": "Small",
            "subCategory": null
          },
          "name": "xyz"
        },
        {
          "room": {
            "number": 2,
            "category": "Small",
            "subCategory": null
          },
          "name": "abc"
        }
      ]
    }
  ]
}

I'm trying to remove duplicates using LINQ - groupby and firstordefault. groupby should be on room - number and category but not on hoteltype. because we need combination of hoteltype, room number and category, if these three combinations are there in JSON that should be eliminated

Please suggest any solutions?

CodePudding user response:

The Distinct() method exists entirely for this purpose.

You can either create a comparer or override Equals in Hotel, but I'll give an example with the latter.

public class Hotel : IEquatable<Hotel>
{
    public string hoteltype { get; set; }
    public List<Room> rooms { get; set; }
    
    public bool Equals(Hotel other)
    {
        if (hoteltype != other.hoteltype) return false;
        if (rooms.Length != other.rooms.Length) return false;
        for (int i = 0; i < rooms.Length; i  )
        {
            if (!rooms[i].Equals(other.rooms[i]) return false;
        }
        return true;
    }
}

You will also have to write an equatable override for the rooms.

public class Room : IEquatable<Room>
{
    public Room2 room { get; set; }
    public string name { get; set; }

    public bool Equals(Room other)
    {
        return name == other.name &&;
               room.Equals(other.room);
    }
}

public class Room2 : IEquatable<Room2>
{
    public int number { get; set; }
    public string category { get; set; }
    public string subCategory { get; set; }

    public bool Equals(Room2 other)
    {
        return number == other.number &&;
               category == other.category &&
               subCategory == other.subCategory;
    }
}

CodePudding user response:

I'm trying to remove duplicates using LINQ - groupby and firstordefault. groupby should be on room - number and category but not on hoteltype. because we need combination of hoteltype, room number and category, if these three combinations are there in JSON that should be eliminated

From your question you require a Linq GroupBy on room => number, room => category .

var grp = rooms.GroupBy(
          room => room.number,
          room => room.category,
          (rm_num, rm_cat) => new
          {
           num = rm_num,
           cat = rm_cat
});
foreach(var result in grp) { 
        Console.WriteLine(result.num " " result.cat);
}
  • Related