Filtering lists using LINQ

前端 未结 9 2205
暖寄归人
暖寄归人 2021-02-04 00:23

I\'ve got a list of People that are returned from an external app and I\'m creating an exclusion list in my local app to give me the option of manually removing people from the

9条回答
  •  没有蜡笔的小新
    2021-02-04 00:37

    var thisList = new List{ "a", "b", "c" };
    var otherList = new List {"a", "b"};
    
    var theOnesThatDontMatch = thisList
            .Where(item=> otherList.All(otherItem=> item != otherItem))
            .ToList();
    
    var theOnesThatDoMatch = thisList
            .Where(item=> otherList.Any(otherItem=> item == otherItem))
            .ToList();
    
    Console.WriteLine("don't match: {0}", string.Join(",", theOnesThatDontMatch));
    Console.WriteLine("do match: {0}", string.Join(",", theOnesThatDoMatch));
    
    //Output:
    //don't match: c
    //do match: a,b
    

    Adapt the list types and lambdas accordingly, and you can filter out anything.

    https://dotnetfiddle.net/6bMCvN

提交回复
热议问题