Tuesday 15 February 2011

linq - C# Removing Duplicates from a List Containing a List -


Say we have a list of "A student", and there is a list of "B Students". Then we add both lists to a more general list, which are called "students", then someone has decided to make our life complex by adding a duplicate list of "A student" to the generic "students" list. What is the most effective way to remove one of the "A student" duplicate lists? Note that this includes two custom classes.

The list of generic students in the code is called lstStudents. This is the list that I would like to remove from any duplicate.

(I tried to come up with a better example, but this is the best I can do now.)

I do not need to use LINQ, but it is available is. MoreLinq is also available.

Here are my classes:

  Students of public class {public student (string _name, int_ _age, exam_sticks) {name = _name; Age = _age; Last xx = _last xm; } The name of the public string {get; Set; } Public int age {get; Set; } The last examination of the public examination {received; Set; }} Public Class Exam {public exam (int _correct, int _possible) {true = _correct; Possible = _possible; } Receive the public int right {}; Set; } Received P {}; Set; }}   

Here's the code to create mess:

  list & lt; List & lt; Students & gt; & Gt; LstStudents = New list & lt; List & lt; Students & gt; & Gt; (); & Lt; Students & gt; LstAStudents = New list & lt; Students & gt; (); & Lt; Students & gt; LstDuplicateAStudents = New list & lt; Students & gt; (); & Lt; Students & gt; LstBStudents = New list & lt; Students & gt; (); // List some students lstAStudents.Add (new student ("Alex", 14, new exam (98,100)); LstAStudents.Add (new student ("Kim", 13, new exam (96, 100)); LstAStudents.Add (new student ("Brian", 14, new exam (9 2, 100))); LstStudents.Add (lstAStudents); // Create a duplicate list of a student lstDuplicateAStudents.Add (new student ("Alex", 14, new exam (98, 100))); LstDuplicateAStudents.Add (new student ("Kim", 13, new exam (96, 100)); LstDuplicateAStudents.Add (new student ("Brian", 14, new exam (9 2, 100))); LstStudents.Add (lstDuplicateAStudents); // Create a list of some B. students lstBStudents.Add (New Student ("John", 13, New Examination (88, 100)); LstBStudents.Add (New Student ("Jenny", 13, New Test (80, 100)); LstBStudents.Add (new student ("Jamie", 15, new exam (81, 100))); LstStudents.Add (lstBStudents);    

Use both of you for both student and Exam can : Public class student: iQquable & lt; Students & gt; {... Public Child Samata (Student Other) {Return Name == Other.name & amp; Age == other. And & amp; Amp; LastExam.Equals (other.lastExam); } Public override bool equivalent (object obj) {student student = obj as student; Return equal (student); } Public Override Int Hashcode () {Return Name. GatehashCode () ^ age.GetHashCode () ^ The last axem. GetHashCode (); }}

Examination :

  For public class exams: IEquatable & lt; Exam & gt; {... Public Bowl Equal (Examination) {Return Examination Correct == right & amp; exam. Possible == possible; } Public override bool equivalent (object obj) {exam exam = exam obj; Return equal (examination); } Public override intoshashcode () {returns correct. GetHashCode () ^ Possible. GetHashCode (); }}   

Then a custom IQualityComparer & lt; T & gt; list for & lt; Students & gt; :

  Public class student list compactor: iCuality Compterper & lt; List & lift; Students & gt; & Gt; {Public Bull Equals (List; Lit; Students; & gt; x, list & amp; lift; student & gt; y) {return x.OrderBy (a => a.name) .equenceEqual (y.OrderBy (b = & Gt; b.name)); } Public intoshashcode (list & lt; student & gt; obje) {return obj.Agregate (0, (current, t) => current ^ t.getHashCode ()); }}   

You can then distinct to get results:

  var results = lstStudents.Distinct (new StudentListComparer ());    

No comments:

Post a Comment