compare two list and return not matching items using linq

后端 未结 10 571
死守一世寂寞
死守一世寂寞 2021-02-01 02:52

i have a two list

List SentList;
List MsgList;

both have the same property called MsgID;

MsgList            


        
10条回答
  •  逝去的感伤
    2021-02-01 02:56

    List persons1 = new List
               {
                        new Person {Id = 1, Name = "Person 1"},
                        new Person {Id = 2, Name = "Person 2"},
                        new Person {Id = 3, Name = "Person 3"},
                        new Person {Id = 4, Name = "Person 4"}
               };
    
    
            List persons2 = new List
               {
                        new Person {Id = 1, Name = "Person 1"},
                        new Person {Id = 2, Name = "Person 2"},
                        new Person {Id = 3, Name = "Person 3"},
                        new Person {Id = 4, Name = "Person 4"},
                        new Person {Id = 5, Name = "Person 5"},
                        new Person {Id = 6, Name = "Person 6"},
                        new Person {Id = 7, Name = "Person 7"}
               };
            var output = (from ps1 in persons1
                          from ps2 in persons2
                          where ps1.Id == ps2.Id
                          select ps2.Name).ToList();
    

    Person class

    public class Person
    {        
        public int Id { get; set; }       
    
        public string Name { get; set; }
    }
    

提交回复
热议问题