我有一個類如下:檢查兩個列表相等
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我的意思是這兩個列表應該包含完全相同的元素......順序沒有關係 –