compare two list and return not matching items using linq

后端 未结 10 543
死守一世寂寞
死守一世寂寞 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:58

    Try,

      public class Sent
    {
        public int MsgID;
        public string Content;
        public int Status;
    
    }
    
    public class Messages
    {
        public int MsgID;
        public string Content;
    }
    
      List SentList = new List() { new Sent() { MsgID = 1, Content = "aaa", Status = 0 }, new Sent() { MsgID = 3, Content = "ccc", Status = 0 } };
                List MsgList = new List() { new Messages() { MsgID = 1, Content = "aaa" }, new Messages() { MsgID = 2, Content = "bbb" }, new Messages() { MsgID = 3, Content = "ccc" }, new Messages() { MsgID = 4, Content = "ffffd" }, new Messages() { MsgID = 5, Content = "eee" }};
    
                int [] sentMsgIDs = SentList.Select(v => v.MsgID).ToArray();
                List result1 = MsgList.Where(o => !sentMsgIDs.Contains(o.MsgID)).ToList();
    

    Hope it should help.

提交回复
热议问题