我有两个类型为CustomerEntity
的对象列表。他们属于同一个实体。
CustomerEntity包含2个字段。
CustomerEntity
ID
Name
List<Customerntity> incomingCustomer
List<CustomerEntity> dbCustomers
我在比较Id
上的2个对象列表时遇到问题。如果incomingCustomer Id存在于dbCustomers中,则将该对象添加到List<CustomerEntity> customersToUpdate
中。
我尝试过linq和for每个语句,但都无法想出一个优雅的解决方案。
尝试:
var tmp = incomingCustomer.Where(x =>
dbCustomers.Any(z => x.Id == z.Id)
&& !dbCustomers.Any(z => x.Id == z.Id));
同样疲惫的前臂环,这是一场灾难
感谢的帮助
获取样本
// These are the class structure
class dbCustomer : CustomerEntity
{
}
class incomingCustomer : CustomerEntity
{
}
class CustomerEntity
{
public int Id { get; set; }
}
// Sample data in list
IList<dbCustomer> dbCustomers = new List<dbCustomer>
{
new dbCustomer{ Id = 2},
new dbCustomer{ Id = 3},
new dbCustomer{ Id = 4}
};
IList<incomingCustomer> incomingCustomer = new List<incomingCustomer>
{
new incomingCustomer{ Id = 1},
new incomingCustomer{ Id = 2},
new incomingCustomer{ Id = 4}
};
// your query
List<CustomerEntity> customersToUpdate = dbCustomers.Where(l1 => incomingCustomer.Any(l2 => l2.Id == l1.Id)).ToList<CustomerEntity>();