Usage Comparison Union and the Contact method in C # -Linq

Linq Union in comparison with the Contact method usage

Beginning of the article, we take a look at the following simple example.

Fragment 1:

int[] ints1 = { 2, 4, 9, 3, 0, 5, 1, 7 };
int[] ints2 = { 1, 3, 6, 4, 4, 9, 5, 0 };

IEnumerable<int> intsUnion = ints1.Union(ints2);
IEnumerable<int> intsContact = ints1.Concat(ints2);

Console.WriteLine("数组ints1:");
foreach (int num in ints1){
    Console.Write("{0} ", num);
}

Console.WriteLine();
Console.WriteLine("数组ints2:");
foreach (int num in ints2){
    Console.Write("{0} ", num);
}

Console.WriteLine();
Console.WriteLine("Union后的结果:");

foreach (int num in intsUnion){
    Console.Write("{0} ", num);
}

Console.WriteLine();
Console.WriteLine("Concat后的结果:");
foreach (int num in intsContact){
    Console.Write("{0} ", num);
}

operation result:

 

From the results, the Contact Union calculated and set methods are two sets, only, Union method returns a result of repeated elements retain only a (set of mathematical operations and consistent set).

Then look at the following example.

Fragment 2:

class Student{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Class { get; set; }
    public int Score { get; set; }

}


List<Student> stuList1 = new List<Student>(){
    new Student(){Id=1,Name="tiana0",Class="04机制春",Score=100},
    new Student(){Id=2,Name="xiaobo",Class="09计研",Score=80},
    new Student(){Id=3,Name="八戒",Class="09计研",Score=30}
};


List<Student> stuList2 = new List<Student>(){
    new Student(){Id=1,Name="tiana0",Class="04机制春",Score=100},
    new Student(){Id=2,Name="张三",Class="09计研",Score=100},
    new Student(){Id=1,Name="八戒",Class="09计研",Score=30}
};


IEnumerable<Student> unionList = stuList1.Union(stuList2);
IEnumerable<Student> concatList = stuList1.Concat(stuList2);
Console.WriteLine("stuList1:Id,Name,Class,Score");
foreach (var s1 in stuList1){
    Console.WriteLine("{0},{1},{2},{3}", s1.Id, s1.Name, s1.Class, s1.Score);
}
Console.WriteLine("stuList2:Id,Name,Class,Score");
foreach (var s2 in stuList2){
    Console.WriteLine("{0},{1},{2},{3}", s2.Id, s2.Name, s2.Class, s2.Score);
}

Console.WriteLine("unionList:Id,Name,Class,Score");
foreach (var s3 in unionList){
    Console.WriteLine("{0},{1},{2},{3}", s3.Id, s3.Name, s3.Class, s3.Score);
}
Console.WriteLine("concatList:Id,Name,Class,Score");
foreach (var s4 in concatList){
    Console.WriteLine("{0},{1},{2},{3}", s4.Id, s4.Name, s4.Class, s4.Score);
}

operation result:

 

See the results, it was found, Union Contact with the method returns to exactly the same result, i.e., Union method does not repeat elements of "de-emphasis" (remove excess retain a) processing.

That in the end, where is the problem?

Read msdn understood, Union method is able to be "de-emphasis" operation, because the Union method by using the default equality comparator generates two sequences and sets. That method uses the default Union equal to the comparator elements is determined, if equal, a "de-duplication" operation.

Further, also learned that, if the desired target sequence from the comparison-defined data type, must implement IEqualityComparer <T> class in the generic interface.

Here, transform their code again.

Fragment 3:

class Student : IEquatable<Student>{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Class { get; set; }
    public int Score { get; set; }
    public bool Equals(Student other) {

        //Check whether the compared object is null.
        if (Object.ReferenceEquals(other, null)) return false;
        //Check whether the compared object references the same data.
        if (Object.ReferenceEquals(this, other)) return true;
        //Check whether the Students' properties are equal.

        return Id.Equals(other.Id) && Name.Equals(other.Name) && Class.Equals(other.Class) && Score.Equals(other.Score);
    }


    // If Equals() returns true for a pair of objects
    // then GetHashCode() must return the same value for these objects.
    public override int GetHashCode(){
        //Get hash code for the Id field.
        int hashStudentId = Id.GetHashCode();

        //Get hash code for the Name field if it is not null.
        int hashStudentName = Name == null ? 0 : Name.GetHashCode();

        //Get hash code for the Class field if it is not null.
        int hashStudentClass = Class == null ? 0 : Class.GetHashCode();

        //Get hash code for the Score field.
        int hashStudentScore = Score.GetHashCode();

        //Calculate the hash code for the Student.
        return hashStudentId ^ hashStudentName ^ hashStudentClass ^ hashStudentScore;

    }

}

Fragment 3 Student modifies only the code to implement IEqualityComparer <T> generic interface.

There is little doubt, obviously telling implement IEqualityComparer <T> generic interface, why Student must achieve IEquatable <Student> Interface, you know? Hope the exhibitions. Because of the time, there is no longer studied, and so next time I studied carefully before a special narrative.

 Run the program again with the following results:

 

Union method but also normal, "de-duplication" of the.

 

Published 16 original articles · won praise 208 · Views 200,000 +

Guess you like

Origin blog.csdn.net/cxu123321/article/details/103509338
Recommended