使用LINQ- C#从多个列表中获取唯一项 [英] Get unique items from multiple lists using LINQ- C#
本文介绍了使用LINQ- C#从多个列表中获取唯一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有3个列表.
ListA<items>
ListB<items>
ListC<items>
我必须从每个列表中选择一个项目,但是item.itemID对于每个项目都应该是唯一的.我怎样才能做到这一点?预先感谢.
I have to select one item from each list but the item.itemID should be unique for each of those items. How can i achieve this? Thanks in advance.
推荐答案
List<items> concat = new List<items>();
concat.AddRange(ListA);
concat.AddRange(ListB);
concat.AddRange(ListC);
List<items> result = new List<items>();
foreach (var item in concat)
{
if (result.Where(x => x.itemId == item.itemId).Count() == 0)
{
result.add(item);
}
}
//result should now contain what you are looking for
这篇关于使用LINQ- C#从多个列表中获取唯一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文