Q
如何比較數組?
0
A
回答
7
var intersection = A.Intersect(B).ToArray();
注意,這裏使用了默認IEqualityComparer<Employee>
它只是將是一個參考比較,除非你覆蓋Equals
和GetHashCode
。或者,您可以實施IEqualityComparer<Employee>
並使用Intersect
的超載,該過載以IEqualityComparer<Employee>
爲例。
0
你可以使用System.Collections.Generic?
我會做這樣的事情:
var listA = new List<Employee>(A);
var listB = new List<Employee>(B); //not sure if arrays implement contains, may not need this line
A = listA.where(e => listB.Contains(e)).toArray();
希望有所幫助。
0
我想你可以從http://msdn.microsoft.com/en-us/library/wdka673a.aspx也可以找到一些靈感方法。你需要把陣列到列表,但不應該難倒你...
0
爲了說明傑森的建議(基於ID的比較):
class IDEmployeeComparer : IEqualityComparer<Employee>
{
public bool Equals(Employee first, Employee second)
{
return (first.ID == second.ID);
}
public int GetHashCode(Employee employee)
{
return employee.ID
}
}
...
var intersection = A.Intersect(B, new IDEmployeeComparer()).ToArray();
Jon Skeet's misc library允許指定所述比較器的內聯,而無需創建一個單獨的類
0
Employee[] c = (from d in a where !b.Contains<Employee>(d) select d).ToArray<Employee>();
相關問題
- 1. 比較數組
- 2. 數組比較?
- 3. 數組比較
- 4. 如何比較數組中的數字
- 5. javascript比較數組
- 6. 比較json數組
- 7. 比較數組值
- 8. 有比較數組
- 9. 比較數組java
- 10. PHP數組比較
- 11. 比較javascript數組
- 12. PHP數組比較
- 13. 我如何比較兩個數組php
- 14. 如何比較兩個數組對象
- 15. PHP:如何比較2數組?
- 16. 如何比較數組內的值
- 17. 如何比較兩個數組列表?
- 18. 如何比較兩個對象數組
- 19. 如何比較int中的int數組
- 20. underscorejs如何比較兩個數組?
- 21. 如何比較Java Byte []數組?
- 22. 如何比較兩個數組的值?
- 23. 如何比較2個字符數組
- 24. 如何讓子集與數組比較
- 25. 如何比較數組中的值
- 26. 如何比較兩個二維數組
- 27. Haskell:如何比較元組?
- 28. 快速比較數組數
- 29. 如何比較的元組組SQL
- 30. 數組和javascript - 如何比較兩個數組
offtop:奇怪標籤 「C#-4.0」。也許「C#4.0」? : - \ – SeeSharp 2011-01-21 05:21:51
@SeeSharp:這是C#4.0的通用標籤。 – jason 2011-01-21 05:22:48
你的意思是屬性而不是屬性? – Simon 2011-01-21 05:26:24