Filtering lists using LINQ

前端 未结 9 2150
暖寄归人
暖寄归人 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:52

    I would do something like this but i bet there is a simpler way. i think the sql from linqtosql would use a select from person Where NOT EXIST(select from your exclusion list)

    static class Program
    {
        public class Person
        {
            public string Key { get; set; }
            public Person(string key)
            {
               Key = key;
            }
        }
        public class NotPerson
        {
            public string Key { get; set; }
            public NotPerson(string key)
            {
               Key = key;
            }
        }
        static void Main()
        {
    
           List persons = new List()
           { 
               new Person ("1"),
               new Person ("2"),
               new Person ("3"),
               new Person ("4")
           };
    
           List notpersons = new List()
           { 
               new NotPerson ("3"),
               new NotPerson ("4")
           };
    
           var filteredResults = from n in persons
                                 where !notpersons.Any(y => n.Key == y.Key)
                                 select n;
    
           foreach (var item in filteredResults)
           {
              Console.WriteLine(item.Key);
           }
        }
     }
    

提交回复
热议问题