我的C#app..A和B中有2个列表集合。
两个集合都有客户对象,其中包含Id和Name属性。通常,A的项目数多于B。
使用Linq,我想只返回ID在A但不在B中的客户。
我该怎么做?
答案 0 :(得分:18)
有多种方法可供选择。如果您覆盖Except
和Equals
,最干净的方法是使用GetHashCode
扩展方法。如果还没有,还有其他选择。
// have you overriden Equals/GetHashCode?
IEnumerable<Customer> resultsA = listA.Except(listB);
// no override of Equals/GetHashCode? Can you provide an IEqualityComparer<Customer>?
IEnumerable<Customer> resultsB = listA.Except(listB, new CustomerComparer()); // Comparer shown below
// no override of Equals/GetHashCode + no IEqualityComparer<Customer> implementation?
IEnumerable<Customer> resultsC = listA.Where(a => !listB.Any(b => b.Id == a.Id));
// are the lists particularly large? perhaps try a hashset approach
HashSet<int> customerIds = new HashSet<int>(listB.Select(b => b.Id).Distinct());
IEnumerable<Customer> resultsD = listA.Where(a => !customerIds.Contains(a.Id));
...
class CustomerComparer : IEqualityComparer<Customer>
{
public bool Equals(Customer x, Customer y)
{
return x.Id.Equals(y.Id);
}
public int GetHashCode(Customer obj)
{
return obj.Id.GetHashCode();
}
}
答案 1 :(得分:5)
如果您为客户对象覆盖等于,则只需使用
A.Except(B);
答案 2 :(得分:4)
扩展为Except,提供您自己的平等,因此您无需更改Equals行为。 我从这里得到了这个:
List<Customer> customersA = new List<Customer> { new Customer { Id = 1, Name = "A" }, new Customer { Id = 2, Name = "B" } };
List<Customer> customersB = new List<Customer> { new Customer { Id = 1, Name = "A" }, new Customer { Id = 3, Name = "C" } };
var c = (from custA in customersA
select custA.Id).Distinct()
.Except((from custB in customersB
select custB.Id).Distinct());