使用LINQ优化两个列表中的比较 [英] optimize the comparison in two lists with LINQ
本文介绍了使用LINQ优化两个列表中的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个对象列表:
客户和员工
我需要检查是否至少有1位与员工同名的客户.
I need to check if there is at least 1 Client with the same name as an employee.
目前,我有:
client.ForEach(a =>
{
if (employee.Any(m => m.Name == a.Name && m.FirstName==a.FirstName)
{
// OK TRUE
}
});
我可以通过其他方式改善阅读吗?
can I improve reading by doing it in another way?
推荐答案
为什么在使用join之前不进行检查?
why won't you check it before hand using join?
var mergedClients = Client.Join(listSFull,
x => new { x.Name, x.FirstName},
y => new { Name = y.Name, FirstName= y.FirstName},
(x, y) => new { x, y }).ToList();
然后遍历新集合:
mergedClients.ForEach(a =>
//your logic
此方法的唯一缺点(如果让您感到困扰)是将不包含空值.
Only disadvantage of this approach (if it bothers you) is that null values will not be included.
这篇关于使用LINQ优化两个列表中的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文