Compare two generic Lists?

Assuming ICollection x and List y If order of the records matters: return x. SequenceEqual(y) If order doesn't matter, I think your best option is to skip LINQ and use a HashSet.

Assuming ICollection x and List y... If order of the records matters: return x. SequenceEqual(y); If order doesn't matter, I think your best option is to skip LINQ and use a HashSet: return new HashSet(x). SetEquals(y).

Caveat though: ICollection x = new List {1}; List y = new List { 1, 1, 1 }; bool areEqual = new HashSet(y). SetEquals(x); //true That may be what you want, but if you want to know if the items in the lists are the same and the items are in the list the same amount of times, this won't work. Kudos though for learning me about SetEquals.

I've never noticed it :) – aquinas Jun 24 at 3:24 A good point (note that an Intersect()-based solution suffers the same fate). If duplicates must be preserved, I'd either sort each list and use SequenceEquals() or group by element and use SetEquals() on item/count pairs. – dahlbyk Jun 24 at 3:31.

Here is a sample code depending on whether sequence is important or not : ICollection collection1 = new List { 5, 1, 6, 7, 3 }; List collection2 = new List { 1, 5, 6, 7, 3 }; bool considerSequence = true; // sequence is important bool areEquael; if (considerSequence) { areEquael = collection1. SequenceEqual(collection2); } else { areEquael = collection1. OrderBy(val => val).

SequenceEqual( collection2. OrderBy(val => val)); } As suggested by other colleagues also consider using HashSet. Just take into account that HashSet is available only starting from .

NET Framework 3.5.

List lst = new List(); List lst1 = new List(); bool result = false; if (lst. Count == lst1. Count) { for (int I = 0; I lst = new List(); lst.

Add(1); lst. Add(51); lst. Add(65); lst.

Add(786); lst. Add(456); List lst1 = new List(); lst1. Add(786); lst1.

Add(1); lst1. Add(456); lst1. Add(65); lst1.

Add(51); bool result = false; if (lst. Count == lst1. Count) { result = lst.

Union(lst1).Count() == lst. Count; } if (result) { Response. Write("list are same"); } else { Response.

Write("list are not same"); } try this one also.....

This assumes that the order of the lists is exactly the same, not just the items in the list. I think the original question was just about set identity. – aquinas Jun 24 at 3:18.

If you want to compare two sequences in . NET 3.5 and beyond (that's the version of the framework which C# 3.0 ships with), then you should use the SeqenceEqual extension method on the Enumerable class in the System. However, for this, it will use the default equality comparer for the type parameter T in the IEnumerable implementations it is comparing.

If you want to change the way that equality is checked for, then you should pass an implementation of IEqualityComparer to the SequenceEqual method to use when comparing the items from each sequence. Also, if you are trying to see if two sets are equal (as represented by IEnumerable) then your best bet is to check use the Except extension method on the Enumerable class and make sure that the result set doesn't have any entries, and that both IEnumerable implementations have the same number of entries (since Except can return an empty sequence even with different length sequences passed to it) which you can do with the Count extension method.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions