[英]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 在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);
这些是正确的答案,但作为一个想法,如果你知道列表将具有相同的数据,但可能是不同的顺序,为什么不只是排序列表,以保证相同的顺序。
var newList = LISTONE.OrderBy(x=>x.[sequence]).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.