tags:

views:

138

answers:

5

The two lists are like

          LISTONE    "ONE", "TWO", "THREE"
          LISTTWO    "ONE", "TWO", "THREE"

i need to compare the whether the items in two lists are in same order or not.

Is there any way to do this in LINQ

+6  A: 

Maybe:

bool equal = collection1.SequenceEqual(collection2);

See also: http://stackoverflow.com/questions/50098/comparing-two-collections-for-equality

mgroves
The `OrderBy` calls would mean that the order was ignored: but Pramodh wants to see if they are in the same order
Kieren Johnstone
yeah sorry, don't know why I initially put those in there :)
mgroves
A: 
List<string> list1;
List<string> list2;

bool sameOrder = list1.SequenceEqual(list2);
Martin Ingvar Kofoed Jensen
+2  A: 

Determine if both lists contain the same data in same order:

bool result = list1.SequenceEqual(list2);

Same entries in different order:

bool result = list1.Intersect(list2).Count() == list1.Count;

Greets Flo

Florian Reischl
Watch out with the second version. The `Intersect` method uses set intersection, so you might get unexpected results if one or both of the lists contain duplicates.
LukeH
Good point. Thanks!
Florian Reischl
A: 

These are the correct answers, but as a thought, If you know the lists will have the same data but may be in different order, Why not just sort the lists to guarantee the same order.

var newList = LISTONE.OrderBy(x=>x.[sequence]).ToList();
Ehsan
+1  A: 

If you know there can't be duplicates:

bool result = a.All(item => a.IndexOf(item) == b.IndexOf(item));

Otherwise

bool result = a.SequenceEquals(b)
Duracell