如何从两个不同的列表中获取常见和不同项目的列表? [英] how can I get a list of common and distinct item from two different list ?
本文介绍了如何从两个不同的列表中获取常见和不同项目的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个不同项目的列表
List1 = ab,bc,ca,bd,kh
List2 = bc,ca,kh,kl
比较list1和list2后我想要一个列表。
输出将显示所有常见项目
List3:bc,ca,kh
有人可以帮我写这段代码吗?
I have two list like of different item
List1=ab,bc,ca,bd,kh
List2=bc,ca,kh,kl
I want a list after comparison of list1 and list2.
the output will show all common item like that
List3:bc,ca,kh
can anyone help me to write this code?
推荐答案
试试这段代码
Try this code
List<string> list3 = (from item in list1
where list2.Contains(item)
select item).ToList();
使用通用列表Intersect()
和除外()
扩展程序:
http://msdn.microsoft.com/en-us/library/vstudio/bb460136%28v=vs.90%29.aspx [ ^ ]
http://msdn.microsoft.com/ en-us / library / vstudio / bb908822%28v = vs.90%29.aspx [ ^ ]
Use the generic listIntersect()
andExcept()
extensions :
http://msdn.microsoft.com/en-us/library/vstudio/bb460136%28v=vs.90%29.aspx[^]
http://msdn.microsoft.com/en-us/library/vstudio/bb908822%28v=vs.90%29.aspx[^]
这篇关于如何从两个不同的列表中获取常见和不同项目的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文