2016-10-14 14 views
-1

https://msdn.microsoft.com/en-us/library/mt693040.aspx以下のコードをlinq経由で比較することができます。左から右、右から左のリストを比較する方法がありますか?csharpの比較リスト<t>右から左へ

class CompareLists 
{   
    static void Main() 
    { 
     // 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<string> 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); 

     // Keep the console window open in debug mode. 
     Console.WriteLine("Press any key to exit"); 
     Console.ReadKey(); 
    } 
} 

/*出力: 次の行がnames1.txtにあるが Potra、クリスティーナ ノリエガ、はFabricio おやおや、カムフー 豊島、ティム ガイ、Weyの元 ガルシアを、names2.txtありませんDebra */

注:左から右は、ソースリストからデスティネーションリストを意味し、逆も同様です。

+0

あなたが見えるように設定されている結果を何を期待しますか? – itsme86

+0

これはあなたが欲しいものですか? IEnumerable differenceQuery = names1.Except(names2).Reverse(); –

+0

この質問を見てください:http://stackoverflow.com/questions/22173762/check-if-two-lists-are-equal – oldovets

答えて

2

Enumerable.Reverse考えてみましょう:

string[] names1 = File.ReadAllLines(@"../../../names1.txt") 
         .Reverse() 
         .ToArray(); 

か、結果を逆に、より効率的である可能性が高い、@JianpingLiuが提案されているよう:

IEnumerable<string> differenceQuery = names1.Except(names2).Reverse(); 
+0

IEnumerable differenceQuery = names1.Except(names2).Reverse(); differenceQueryはname1のサブセットであるため、より優れています。 –

+0

名前1と名前2をどのようにソートしても、結果には何の違いもありません。 –

+0

私はこれがOPが求めていることとは関係ないと思っています... – itsme86

1

はあなたが内のテキストをしたいわけでくださいnames2ではなく、names1にありますか?あなたはnames1の交差外のすべてを探しているならnames2.Except(names1)

を試してみて、この答えをチェックしnames2場合The opposite of Intersect()

+0

リストが(文字列、int、日付時刻を合わせて)型かどうかを調べようとしています。私はソースと宛先のリストと宛先とソースリストの違いを比較しようとしています。 – Kurkula

関連する問題