|
|
In this post, we will see how to compare two List<string> and list the elements found in one List<string> but not in the other. We will be using the Enumerable.Except method
C#
static void Main(string[] args)
{
List<string> lstOne =
new List<string>() { "Jim", "Jack", "Kate", "Nope" };
List<string> lstTwo =
new List<string>() { "Jack", "Nope", "Jim" };
IEnumerable<string> lstNew = null;
// Compare two List<string> and display items of lstOne not in lstTwo
lstNew = lstOne.Except(lstTwo, StringComparer.OrdinalIgnoreCase);
PrintList(lstNew);
Console.ReadLine();
}
static void PrintList(IEnumerable<string> str)
{
foreach (var s in str)
Console.WriteLine(s);
Console.WriteLine("-------------");
}
OUTPUT
Read some more tips in my article over here Some Common Operations using List<string>
'Like' us on our FaceBook page if you find this blog useful. Thanks!
Did you like this post?
|
|
|
||
|
|
|
|
Save on Delicious |
|
|
subscribe via rss |
|
subscribe via e-mail |
|
|
print this post |
|
follow me on twitter |





comments
0 Responses to "Compare two List<string> using LINQ"Post a Comment