Remove elements from one List that are found in another

后端 未结 8 979
情话喂你
情话喂你 2020-12-10 11:31

I have two lists

 List list1 = new List();
 List list2 = new List();

I want remove all elements from li

相关标签:
8条回答
  • 2020-12-10 11:56
            var NewList = FirstList.Where(a => SecondList.Exists(b => b.ID != a.ID));
    

    Using LINQ

    0 讨论(0)
  • 2020-12-10 12:01

    Description

    I think you mean the generic type List<Type>. You can use Linq to do this

    Sample

    List<string> l = new List<string>();
    List<string> l2 = new List<string>();
    
    l.Add("one");
    l.Add("two");
    l.Add("three");
    
    l2.Add("one");
    l2.Add("two");
    l2.Add("three");
    l2.Add("four");
    
    l2.RemoveAll(x => l.Contains(x));
    

    More Information

    • MSDN - List.RemoveAll Method
    0 讨论(0)
提交回复
热议问题