如何使用C#中的LINQ比较两个列表<string>[英] How to compare two List<String> using LINQ in C#

本文是小编为大家收集整理的关于如何使用C#中的LINQ比较两个列表<string>的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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)
相关标签/搜索