问题描述
这两个列表就像
LISTONE "ONE", "TWO", "THREE" LISTTWO "ONE", "TWO", "THREE"
我需要比较两个列表中的项目是否以相同的顺序.
有什么办法可以在Linq
中执行此操作推荐答案
也许:
bool equal = collection1.SequenceEqual(collection2);
另请参见:比较两个集合以等于等于
其他推荐答案
确定两个列表是否按相同顺序包含相同的数据:
bool result = list1.SequenceEqual(list2);
以不同顺序相同的条目:
bool result = list1.Intersect(list2).Count() == list1.Count;
其他推荐答案
如果您知道不能重复:
bool result = a.All(item => a.IndexOf(item) == b.IndexOf(item));
否则
bool result = a.SequenceEquals(b)
问题描述
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)