开发者

How to compare two List<String> using LINQ in C#

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


Maybe:

bool equal = collection1.SequenceEqual(collection2);

See also: Comparing two collections for equality irrespective of the order of items in them


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;


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)


List<string> list1;
List<string> list2;

bool sameOrder = list1.SequenceEqual(list2);


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜