C# csharp比较列表从左到右和从右到左

C# csharp比较列表从左到右和从右到左,c#,list-comparison,C#,List Comparison,通过linq,可以使用下面的代码对来自字符串列表的字符串进行比较。是否有任何内置的方法来比较列表从左到右和从右到左 class CompareLists { static void Main() { // Create the IEnumerable data sources. string[] names1 = System.IO.File.ReadAllLines(@"../../../names1.txt");

通过linq,可以使用下面的代码对来自字符串列表的字符串进行比较。是否有任何内置的方法来比较列表从左到右和从右到左

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中,但不在names2.txt中 波特拉,克里斯蒂娜 法布里西奥诺列加 哦,金福 蒂姆,丰岛 盖伊,韦远 加西亚,黛布拉 */

注意:从左到右表示源列表到目标列表,反之亦然。

请考虑:

或者,可能更有效地扭转结果,正如@JianpingLiu所建议的:

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

您的意思是希望文本在名称2中,而不是在名称1中?如果是,请尝试名称2.Exceptnames1


如果您正在查找名称1和名称2相交之外的所有内容,请检查此答案

您希望结果集是什么样子?这是您想要的吗?IEnumerable differenceQuery=names1.Exceptnames2.Reverse;看看这个问题:IEnumerable differenceQuery=names1.Exceptnames2.Reverse;更好,因为differenceQuery是name1的子集。无论您如何对name1和names 2进行排序,它对结果都没有影响。@itsme86可能是。你对这个问题的理解是什么?@AlexD我想他们对两个不同的结果感兴趣。一个显示列表1中缺少的列表2中的项目,另一个显示列表2中缺少的列表1中的项目。我还认为他们试图把这些数据作为一个结果集,可能吗?这还不清楚,OP似乎不愿意回答我的问题。我想看看列表是否为string、int和date-time类型。我试图比较源和目标列表以及目标和源列表的差异。
IEnumerable<string> differenceQuery = names1.Except(names2).Reverse();