Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Using linq let me know how to compare 2 list of strings.. i oly want to know whether they are equal
Posted
Updated 12-Jun-11 22:48pm
v2
Comments
Sergey Alexandrovich Kryukov 13-Jun-11 4:47am    
Change a tag Silverlight to .NET, and add LINQ (OK, I did it for you).
Always try to tag accurately, to get better answer sooner.
--SA

By equal I expect you mean whether the two lists have the same values, you can use the Except operator and check whether the result has a count of zero (as pointed out by Tarun, you need to check whether both ways are the same ), something like this

C#
List<string> one = new List<string>() { "a", "b", "c" };
           List<string> two = new List<string>() { "a", "b", "c", "d" };
           //If both have same values
           if (one.Except(two).Count() == 0 && two.Except(one).Count() == 0)
           {
               MessageBox.Show("They have same values");
           }
           else
           {
               MessageBox.Show("They are not same.");
           }
           //To ignore case
           if (one.Except(two, StringComparer.CurrentCultureIgnoreCase).Count() == 0 && two.Except(one, StringComparer.CurrentCultureIgnoreCase).Count() == 0)
           {
               MessageBox.Show("They have same values, igoring case");
           }
           else
           {
               MessageBox.Show("They are not same.");
           }
           //If one is same reference as two
           if (one.Equals(two))
           {
               MessageBox.Show("They are the same reference");
           }
           else
           {
               MessageBox.Show("They are not same reference");
           }



Hope this helps
 
Share this answer
 
v2
Comments
Tarun.K.S 13-Jun-11 5:24am    
Hi Wayne, when I added a "d" in List two, it still shows they have same values, which I think shouldn't be the case or am I missing something?
Wayne Gaylard 13-Jun-11 5:33am    
You are right, I adjusted my answer. Thanks.
Tarun.K.S 13-Jun-11 5:43am    
I have one more doubt. If I have list1 = "a","b","c","a" and list2 = "a","b","c". It will give the result as equal though the number of elements(4 in list1 and 3 in list2) are different in both the cases. I think that's fine. What's your take on this?
Tarun.K.S 13-Jun-11 5:55am    
Have a 5, I did learn something new today.
Wayne Gaylard 13-Jun-11 6:26am    
Your scenario with 2 of the same element is new to me. I will have to think about it. Thanks for the vote!
Try:
C#
List<string> ListA = new List<string>("hello paul this is a text string".Split(' '));
List<string> ListB = new List<string>("hello paul this is a text string again".Split(' '));
var differences = ListA.Where(x => !ListB.Any(x1 => x1 == x))
.Union(ListB.Where(x => !ListA.Any(x1 => x1 == x)));
if (differences.Count() != 0)
    {
    Console.WriteLine("Different!");
    }
 
Share this answer
 
Comments
Wayne Gaylard 13-Jun-11 5:09am    
You can use the Except Operator and check whether result is 0. See my answer.
static void Main(string[] args)
{
    List<string> List1 = new List<string> { "Car", "Home", "City" };
    List<string> List2 = new List<string> { "Car", "Home", "City" };
    List<string> List3 = new List<string> { "Home", "Car", "City" };
    Console.WriteLine(List1.SequenceEqual(List2));//Result is True
    Console.WriteLine(List1.SequenceEqual(List3));//Result is False

    Console.Read();
}
 
Share this answer
 
v2
Comments
Wayne Gaylard 13-Jun-11 5:11am    
SequenceEqual will only return true if the elements are in exactly the same order. Equals returns true only if both return the same reference.
alrosan 13-Jun-11 5:16am    
No try this
List<string> List1 = new List<string> { "Car", "Home", "City" };
List<string> List2 = new List<string> { "Car", "Home", "City","Country" };
Console.WriteLine(List1.SequenceEqual(List2));//Result is False
Wayne Gaylard 13-Jun-11 5:20am    
Yes, but the List's have to be both the same number of elements, and the elements have to be in exactly the same order.
alrosan 13-Jun-11 5:24am    
give me an example plz
Tarun.K.S 13-Jun-11 5:30am    
Although list1 and list3 have the same elements but the order is not correct, the result should be true but in your case, its coming as false which is wrong.
Hey thanks a lot.. It helped but the thing is that it compares only when count in both the list is same.. No problem,i added one more condition for that..

N please let me know any good source to learn silverlight.. I want to learn in around 45 days.. I can spend 5 hrs per day and more on weekends.. But i want to.. Please do let me know how its possible.. With that i also need to learn Linq in silverlight
 
Share this answer
 
Comments
Tarun.K.S 13-Jun-11 5:54am    
Hi, you can learn silverlight from CP articles here : http://www.codeproject.com/KB/silverlight/. Also have a look at these articles by Kunal : http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=6177366.
Also do vote and accept the answer(s) that helped. Good luck.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900