c#Linq 取2个集合的交集差集并集原创
金蝶云社区-疯狂的石头
疯狂的石头
1人赞赏了该文章 1,060次浏览 未经作者许可,禁止转载编辑于2021年02月22日 16:52:22


    public class Inventory

    {

        int Agt;


        public int Agt1

        {

            get { return Agt; }

            set { Agt = value; }

        }

        string Name;


        public string Name1

        {

            get { return Name; }

            set { Name = value; }

        }

    }




private void Form1_Load(object sender, EventArgs e)

        {

            

 Inventory Inv1 = new Inventory();

            Inv1.Agt1 = 1;

            Inv1.Name1 = "测试1";


            Inventory Inv2 = new Inventory();

            Inv2.Agt1 = 2;

            Inv2.Name1 = "测试2";


            Inventory Inv3 = new Inventory();

            Inv3.Agt1 = 3;

            Inv3.Name1 = "测试3";


            List<Inventory> list1 = new List<Inventory>();

            list1.Add(Inv1);

            list1.Add(Inv2);

            list1.Add(Inv3);



            Inventory Inv11 = new Inventory();

            Inv11.Agt1 = 11;

            Inv11.Name1 = "测试11";


            Inventory Inv22 = new Inventory();

            Inv22.Agt1 = 22;

            Inv22.Name1 = "测试22";



            List<Inventory> list2 = new List<Inventory>();

            list2.Add(Inv11);

            list2.Add(Inv22);

            list2.Add(Inv1);


            //(取list1中有,LIst2中没有的)。

            List<Inventory> list3 = list1.Except(list2).ToList();

            

             //list1, list2的交集 

            List<Inventory> list3 = list2.Intersect(list1).ToList();


           //list1, list2的并(全)集 

            var list3 = list1.Union(list2).ToList();



        }    

图标赞 1
1人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!

您的鼓励与嘉奖将成为创作者们前进的动力,如果觉得本文还不错,可以给予作者创作打赏哦!

请选择打赏金币数 *

10金币20金币30金币40金币50金币60金币
可用金币: 0