首页 > Windows开发 > 详细

[转载]C# List<T>的并集、交集、差集

时间:2017-09-22 17:09:45      阅读:246      评论:0      收藏:0      [点我收藏+]

原文地址:http://www.cnblogs.com/godbell/p/7535637.html

集合的并集是合并集合的项,如下图所示:

技术分享

技术分享
List<int> ls1 = new List<int>() { 1,2,3,5,7,9 };
List<int> ls2 = new List<int>() { 2,4,6,8,9,10};

IEnumerable<int> unionLs = ls1.Union(ls2);
foreach (int item in unionLs)
{
    Console.Write("{0}\t", item);
}
技术分享

技术分享

集合的交集是取集合的共同的项,如下图所示:

技术分享

 

技术分享
List<int> ls1 = new List<int>() { 1,2,3,5,7,9 };
List<int> ls2 = new List<int>() { 2,4,6,8,9,10};


IEnumerable<int> intersectLs = ls1.Intersect(ls2);
foreach (int item in intersectLs)
{
    Console.Write("{0}\t",item);
}
技术分享

技术分享

集合的差集是取在该集合中而不在另一集合中的所有的项,如下图所示:

技术分享

 

技术分享
List<int> ls1 = new List<int>() { 1,2,3,5,7,9 };
List<int> ls2 = new List<int>() { 2,4,6,8,9,10};


IEnumerable<int> exceptLs = ls1.Except(ls2);
foreach (int item in exceptLs)
{
    Console.Write("{0}\t", item);
}
技术分享

技术分享

 

[转载]C# List<T>的并集、交集、差集

原文:http://www.cnblogs.com/FingerCaster/p/7575679.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!