-1

from https://msdn.microsoft.com/en-us/library/mt693040.aspx list of strings can be compared using the below code via linq. is there any built in way to compare list left to right and right to left?

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();
    }
}

/* Output: The following lines are in names1.txt but not names2.txt Potra, Cristina Noriega, Fabricio Aw, Kam Foo Toyoshima, Tim Guy, Wey Yuan Garcia, Debra */

Note: Left to Right means source list to destination list and vice versa.

Kurkula
  • 6,386
  • 27
  • 127
  • 202

2 Answers2

2

Consider Enumerable.Reverse:

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

Or, likely to be more efficient, to reverse the result, as @JianpingLiu suggested:

IEnumerable<string> differenceQuery = names1.Except(names2).Reverse();
AlexD
  • 32,156
  • 3
  • 71
  • 65
  • IEnumerable differenceQuery = names1.Except(names2).Reverse(); is better since differenceQuery is a subset of name1. – Liu Oct 14 '16 at 22:08
  • no matter how you sort the names1 and names 2, It makes no difference to the result. – Liu Oct 14 '16 at 22:13
  • @itsme86 Could be. What is your understanding of the question? – AlexD Oct 14 '16 at 22:25
  • @AlexD I think they're interested in 2 separate results. One showing items in list1 that are missing in list2 and the second one that shows items in list2 that are missing in list1. I also think they're trying to get that data as one result set possibly? That's unclear, and the OP seems unwilling to answer my question about that. – itsme86 Oct 14 '16 at 22:29
1

Do you mean you want the text in names2 but not in names1? If so try names2.Except(names1)

If you are looking for the everything outside the intersect of names1 and names2 check this answer The opposite of Intersect()

Community
  • 1
  • 1
KMoussa
  • 1,568
  • 7
  • 11
  • I am trying to see if list is of type (string, int and date time together). I am trying to compare source and destination lists and destination and source list s for differences. – Kurkula Oct 14 '16 at 23:11