Comparing two collections for equality irrespective of the order of items in them

后端 未结 19 2103
我在风中等你
我在风中等你 2020-11-22 10:28

I would like to compare two collections (in C#), but I\'m not sure of the best way to implement this efficiently.

I\'ve read the other thread about Enumerable.Sequen

19条回答
  •  鱼传尺愫
    2020-11-22 10:59

    Why not use .Except()

    // Create the IEnumerable data sources.
    string[] names1 = System.IO.File.ReadAllLines(@"../../../names1.txt");
    string[] names2 = System.IO.File.ReadAllLines(@"../../../names2.txt");
    // Create the query. Note that method syntax must be used here.
    IEnumerable differenceQuery =   names1.Except(names2);
    // Execute the query.
    Console.WriteLine("The following lines are in names1.txt but not names2.txt");
    foreach (string s in differenceQuery)
         Console.WriteLine(s);
    

    http://msdn.microsoft.com/en-us/library/bb397894.aspx

提交回复
热议问题