This C# LINQ example shows you how to compare two lists of strings and output those lines that are in names1.txt but not in names2.txt.
// ----------------------------------------------------------------------------
// Find the Set Difference Between Two Lists
// https://msdn.microsoft.com/en-us/library/mt693040.aspx
//
// This example shows how to use LINQ to compare two lists of strings and output
// those lines that are in names1.txt but not in names2.txt.
//
// To create the data files:
//
// Copy names1.txt and names2.txt to your solution folder as shown in
// How to: Combine and Compare String Collections (LINQ) (C#)
// <https://msdn.microsoft.com/en-us/library/mt693089.aspx>
// ----------------------------------------------------------------------------
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
*/