-2

Removing elements of a list of class having some specific duplicate properties

I'll try to explain with a example C# code-

There's a List<A> where A is a class

Class A
{
 Int Id{get; set;}
 String name{ get; set;}
 B b{get; set;} // B is a Class
}
Class B
{
 Int sId{get; set;}
 String detail{get; set;}
}

Now List<A> contains a list of elements. I have to remove those specific duplicates from this list whose Id and sId have been there in the list once. So its just a Removal of duplicates from a list but wrt these two properties.

Any help in this would be appreciated

Adding more details for better explanation

Consider this Json data representing my above Code-

A{
   Id: 1
   Name:  Harry
   b
    { 
      sId: 1
      detail: Book
     },

    Id: 2
   Name:  Harry
   b
    { 
      sId: 2
      detail: Book
     }

     Id: 1
   Name:  Russel
   b
    { 
      sId: 1
      detail: Biography
     }

Output which I want-

A{
   Id: 1
   Name:  Harry
   b
    { 
      sId: 1
      detail: Book
     },

    Id: 2
   Name:  Harry
   b
    { 
      sId: 2
      detail: Book
    }

Also would like to tell that this basic approach is working fine-

int a;
int b;
for (int i = 0; i < A.Count; i++)
{
a = A[i].Id;
b = A[i].b.sId;
for (int j = i + 1; j < (A.Count-1); j++)
{
if ((A[j].Id == a) && (A[j].b.sId == b))
{
A.RemoveAt(j);
}
}
}

But It would be great to have a better approach to do this. Maybe a Linq or other

Would need help on that... Thank You

Alex Rudd
  • 9
  • 1
  • 2
    Your example C# code, is not actually C# code, and full of mistakes. your json is not actually json and full of mistakes, and the C# you did include is not formatted very well – TheGeneral Sep 27 '21 at 06:53
  • 2
    "But It would be great to have a better approach" what do you consider "better"? Less lines of code? Faster? ... ? If the working one does it's job, what exactly do you need? – MakePeaceGreatAgain Sep 27 '21 at 06:53
  • 1
    This is the perfect opportunity to get out LinqPad and start writing it yourself, here's the canonical guide with tonnes of examples https://learn.microsoft.com/en-us/samples/dotnet/try-samples/101-linq-samples/ – Jeremy Thompson Sep 27 '21 at 06:56
  • You could use Group by: https://stackoverflow.com/questions/489258/linqs-distinct-on-a-particular-property – Sebastian Siemens Sep 27 '21 at 07:00

1 Answers1

1

First of all, your code which is supposed to be C# code is not correct, but nevertheless the idea is clear. Here's what you need to do:

Have a better versions of your A & B classes to at least compile and meet basic C# coding guidelines:

    public class A
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public B B { get; set; }
    }

    public class B
    {
        public int SId { get; set; }
        public string Detail { get; set; }
    }

Here's your desired line of code to get your filtered result (assuming the collection to filter is named collectionOfAs):

var res = collectionOfAs.Distinct(new Comparer());

And here's the "Comparer" you can use:

    public class Comparer : IEqualityComparer<A>
    {
        public bool Equals(A x, A y)
        {
            return x.Id == y.Id && x.B.SId == y.B.SId;
        }

        public int GetHashCode(A obj)
        {
            return obj.Id.GetHashCode() + obj.B.SId.GetHashCode();
        }
    }
David Oganov
  • 976
  • 1
  • 11
  • 23