简体   繁体   English

在C#中通过LINQ比较两个列表?

[英]Compare two lists via LINQ in c#?

if (!list.SequenceEqual(new TESTList()))
{
     MessageBox.Show("updated");
}

Pretty much this code does not seem to work. 这段代码似乎几乎不起作用。

I have a class as so; 我有这样的课程。

class TESTList
{
private bool test;
}

Now, assuming my code all works and list is properly created, how can I determine if the two lists are the same by checking each item in the list (its boolean) and determing overall taht if both items (ex. item1 list1 and item1 list2) have same booleans, then both lists same. 现在,假设我的代码所有工作和列表均正确创建,那么如何通过检查列表中的每个项目(其布尔值)并确定两个项目(例如item1 list1和item1 list2)的总体值来确定两个列表是否相同)具有相同的布尔值,则两个列表均相同。

I don't know. 我不知道。

Be careful with Enumerable.Zip recommendation as it can result in true where the 2 input sequences are of different lengths but match up to the point where the shorter sequence ends. 要小心Enumerable.Zip推荐,因为它会导致true在2个输入序列具有不同长度,但匹配到较短序列结束点。 It will even result in true if one of the sequences is empty and the other is not! 如果其中一个序列为空而另一个序列不为空,则甚至会为true

result here will be true : result将是true

var list1 = new List<int> { 1, 2, 3 };
var list2 = new List<int> { 1, 2, 3, 4 };

bool result = Enumerable.Zip(list1, list2, (left, right) => left == right).All(x => x);

Instead you might want to use SequenceEqual : 相反,您可能想使用SequenceEqual

bool result = list1.SequenceEqual(list2);

Results: 结果:

var list1 = new List<int> { 1, 2, 3 };
var list2 = new List<int> { 1, 2, 3 };

TRUE 真正

var list1 = new List<int> { 1, 2, 3 };
var list2 = new List<int> { 1, 2, 3, 4 };

FALSE

If you require a result of true when the sequences contain the same elements in any order you need to sort both sequences before comparing them: 如果在序列以任何顺序包含相同元素时都要求结果为true ,则需要在比较它们之前对两个序列进行排序:

bool result = list1.OrderBy (l => l).SequenceEqual(list2.OrderBy (l => l));

You should note that SequenceEqual uses the default equality comparer for the type inside the list. 您应该注意, SequenceEqual使用默认的相等比较器作为列表内的类型。 This will work out of the box for a bool but you may need to override Equals if your list contains a custom class which is only differentiated by a private field (I suspect your final code won't really look like that). 这对于bool是开箱即用的,但是如果您的列表包含仅由私有字段区分的自定义类,则您可能需要覆盖Equals (我怀疑您的最终代码看上去并不是真的)。

bool result = Enumerable.Zip(list1, list2, (left, right) => left == right).All(x => x);

Given that this is a custom class (and what's more, the test field is private), there's no possible way as written for LINQ to check equality properly, so it would need to fall back on reference-equality, where the same memory addresses are being pointed to by each list. 鉴于这是一个自定义类(而且test字段是私有的),因此没有为LINQ编写的方法来正确地检查相等性,因此它需要依靠引用相等性,其中相同的内存地址是被每个列表指向。 There's no particular reason to suppose this to be the case. 没有特殊理由可以假设是这种情况。

You'll need to implement a custom Equals method in TESTlist , therefore. 因此,您需要在TESTlist实现自定义的Equals方法。 (Untested.) (未经测试)。

  public overrides bool Equals(object other) {
    var tlo = other as TESTlist;
    if (tlo != null) {
      return tlo.test == this.test;
    }
    else {
      return false;     // It's not a TESTlist, it can't be equal
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM