使用法の比較連合とC#-Linqでの連絡方法

連絡方法の使用と比較してLINQの連合

記事の冒頭、私たちは、次の簡単な例を見てみましょう。

フラグメント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);
}

結果:

 

結果から、接触ユニオンを算出し、設定方法は、2つのセットであり、唯一、ユニオン方法戻る繰り返し要素の結果は、(数学演算と一致するセットのセット)を保持します。

そして、次の例を見てみましょう。

フラグメント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);
}

結果:

 

それが全く同じ結果、即ち、ユニオン方法が「デエンファシス」の繰り返し要素(削除過剰保持)しない処理する方法戻ると連合接触、発見された、結果を表示します。

最後に、どこに問題があることを?

読むのMSDNには、デフォルトの平等のコンパレータを使用して、連合の方法は二つの配列とのセットを生成するので、組合方式は、「デエンファシス」操作であることが可能である、理解しました。デフォルトユニオンコンパレータ要素に等しく、その方法の用途が等しい場合は、「重複除外」操作、決定されます。

さらに、また、比較定義データ型から所望の標的配列あれば、されたIEqualityComparer <T>ジェネリックインターフェイスでクラスを実装しなければならない、ということを学びました。

ここで、再び自分のコードを変換します。

断片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;

    }

}

断片3学生修正されたIEqualityComparer <T>ジェネリックインターフェイスを実装するコードのみ。

ほとんど疑いは明らかにあなたが知っている、なぜ学生がIEquatable <学生>インタフェースを達成しなければならない、されたIEqualityComparer <T>ジェネリックインターフェイスを実装して伝える、あるのでしょうか?展覧会を願っています。そのため、時間の、もはやそこに研究されていない、と次回ので、私は特別な物語の前に、慎重に検討を行いました。

 以下の結果で再度プログラムを実行します。

 

連合の方法だけでなく、通常の、「重複」。

 

16元記事公開 ウォンの賞賛208 ビューに20万+を

おすすめ

転載: blog.csdn.net/cxu123321/article/details/103509338