首页 > 代码库 > Lambda

Lambda

 

1、在list0中移除list1

            List<int> list0 = new List<int>{ 0,1,2,3,4,5};
            List<int> list1= new List<int> { 0, 1, 2 };

      
            //list0.RemoveAll(p => list1.Contains(p));
            
            list0.RemoveAll(p =>
            {
                if (list1.Contains(p)) return true;
                return false;
            });

            Assert.AreEqual(list0.Count, 3);
            Assert.AreEqual(list0[0], 3);
            Assert.AreEqual(list0[1], 4);
            Assert.AreEqual(list0[2], 5);

参考

listA:
      id:1 , date:2016/11/20  
      id:2 , date:2016/11/20
listB:
      id:1 , date:2016/11/20  
      id:2 , date:2016/11/21
      id:3 , date:2016/11/21

要求删选出listB中 date>A中的date,以及listB中有但是listA中没有的数据,删选后数据应该如下
listC:
      id:2 , date:2016/11/21           date大于A中的date
      id:3 , date:2016/11/21           A中没有

var query = listB.Where(p =>
            {
                if (!listA.Any(s => s.id == p.id)) return true;
                if (listA.Any(s=>s.id == p.id && s.date < p.date)) return true;
                return false;
            });

 

————————————————————————————————————————————————————————

Lambda