我有一个类如下:检查两个列表相等
public class Tag {
public Int32 Id { get; set; }
public String Name { get; set; }
}
我有标签的两个表:
List<Tag> tags1;
List<Tag> tags2;
我用LINQ的选择让每个IDS标签列表。然后:
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不应该等于ids4和ids5 ...
我试过如下:
var a = ints1.Equals(ints2);
var b = ints1.Equals(ints3);
但两者给我假的。
什么是检查标签列表是否相等的最快方法?
UPDATE
我要寻找哪些职位标签是完全一样的一本书的标记。
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();
我使用Entity Framework,我得到的错误:
An exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code
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.
如何解决这个问题?
你说的不是exacly平等是指,你的意思是所有的元素应该是不同的,或者只是它们不应该包含相同的元素,至少应该有一个不同的元素? –
您的序列'ids5'包含重复项。那是故意的吗? – dasblinkenlight
@ Selman22我的意思是这两个列表应该包含完全相同的元素......顺序没有关系 –