检查两个列表相等 [英] Check if two lists are equal

查看:166
本文介绍了检查两个列表相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类如下:

public class Tag {
  public Int32 Id { get; set; }
  public String Name { get; set; }
}

和我有标签的两个名单:

And I have two lists of tag:

List<Tag> tags1;
List<Tag> tags2;



我用的 LINQ 的选择让每个标签列表的ID。然后:

I used LINQ's select to get the Ids of each tags list. And then:

  List<Int32> ids1 = new List<Int32> { 1, 2, 3, 4 };

  List<Int32> ids2 = new List<Int32> { 1, 2, 3, 4 };

  List<Int32> ids3 = new List<Int32> { 2, 1, 3, 4 };

  List<Int32> ids4 = new List<Int32> { 1, 2, 3, 5 };

  List<Int32> ids5 = new List<Int32> { 1, 1, 3, 4 };



IDS1应等于IDS2和ids3 ......都具有相同的编号。

ids1 should be equal to ids2 and ids3 ... Both have the same numbers.

IDS1不应该等于ids4和ids5 ...

ids1 should not be equal to ids4 and to ids5 ...

我试过如下:

  var a = ints1.Equals(ints2);

  var b = ints1.Equals(ints3);



但两者给我假的。

But both give me false.

什么是检查标签的列表是相等的最快方法是什么?

What is the fastest way to check if the lists of tags are equal?

更新

我要寻找哪些职位标签是完全一样的一本书的标记。

I am looking for POSTS which TAGS are exactly the same as the TAGS in a BOOK.

IRepository repository = new Repository(new Context());

IList<Tags> tags = new List<Tag> { new Tag { Id = 1 }, new Tag { Id = 2 } };

Book book = new Book { Tags = new List<Tag> { new Tag { Id = 1 }, new Tag { Id = 2 } } };

var posts = repository
  .Include<Post>(x => x.Tags)
  .Where(x => new HashSet<Int32>(tags.Select(y => y.Id)).SetEquals(book.Tags.Select(y => y.Id)))
  .ToList();



我使用的实体框架和我得到的错误:

System.NotSupportedException类型的异常出现在mscorlib中.dll文件,但不是在用户代码处理

An exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code

其他信息:LINQ到实体无​​法识别方法'布尔SetEquals(System.Collections.Generic.IEnumerable`1 [系统.Int32])的方法,而这种方法不能被翻译成店的表情。

Additional information: LINQ to Entities does not recognize the method 'Boolean SetEquals(System.Collections.Generic.IEnumerable`1[System.Int32])' method, and this method cannot be translated into a store expression.

我要如何解决这个问题?

How do I solve this?

推荐答案

列表< T> 平等不检查它们的元素乘元素。您可以使用 LINQ的 SequenceEqual 该方法

List<T> equality does not check them element-by-element. You can use LINQ's SequenceEqual method for that:

var a = ints1.SequenceEqual(ints2);

要忽略顺序,使用 SetEquals

var a = new HashSet<int>(ints1).SetEquals(ints2);

这应该工作,因为你是在比较的ID,不包含重复的序列。如果是这样,你需要采取重复考虑进去,做线性时间的方式是构成计数基于散列的字典中,添加一个第一个序列中的每个元素,减去一个第二的每个元素序列,并检查产生的数都是零:

This should work, because you are comparing sequences of IDs, which do not contain duplicates. If it does, and you need to take duplicates into account, the way to do it in linear time is to compose a hash-based dictionary of counts, add one for each element of the first sequence, subtract one for each element of the second sequence, and check if the resultant counts are all zeros:

var counts = ints1
    .GroupBy(v => v)
    .ToDictionary(g => g.Key, g => g.Count());
var ok = true;
foreach (var n in ints2) {
    int c;
    if (counts.TryGetValue(n, out c)) {
        counts[n] = c-1;
    } else {
        ok = false;
        break;
    }
}
var res = ok && counts.Values.All(c => c == 0);



最后,如果你的罚款与 O(N * LOGN)解决方案,可以将两个序列进行排序,并使用 SequenceEqual 比较它们是否相等。

Finally, if you are fine with an O(N*LogN) solution, you can sort the two sequences, and compare them for equality using SequenceEqual.

这篇关于检查两个列表相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆