compare two list and return not matching items using linq

后端 未结 10 544
死守一世寂寞
死守一世寂寞 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<Person> persons1 = new List<Person>
               {
                        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<Person> persons2 = new List<Person>
               {
                        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; }
    }
    
    0 讨论(0)
  • 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<Sent> SentList = new List<Sent>() { new Sent() { MsgID = 1, Content = "aaa", Status = 0 }, new Sent() { MsgID = 3, Content = "ccc", Status = 0 } };
                List<Messages> MsgList = new List<Messages>() { 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<Messages> result1 = MsgList.Where(o => !sentMsgIDs.Contains(o.MsgID)).ToList<Messages>();
    

    Hope it should help.

    0 讨论(0)
  • 2021-02-01 03:01

    You can do like this,this is the quickest process

    Var result = MsgList.Except(MsgList.Where(o => SentList.Select(s => s.MsgID).ToList().Contains(o.MsgID))).ToList();
    

    This will give you expected output.

    0 讨论(0)
  • 2021-02-01 03:05

    You can do something like

    var notSent = MsgSent.Except(MsgList, MsgIdEqualityComparer);
    

    You will need to provide a custom equality comparer as outlined on MSDN

    http://msdn.microsoft.com/en-us/library/bb336390.aspx

    Simply have that equality comparer base equality only on MsgID property of each respective type. Since the equality comparer compares two instances of the same type, you would need to define an interface or common base type that both Sent and Messages implement that has a MsgID property.

    0 讨论(0)
  • 2021-02-01 03:06

    The naive approach:

    MsgList.Where(x => !SentList.Any(y => y.MsgID == x.MsgID))
    

    Be aware this will take up to m*n operations as it compares every MsgID in SentList to each in MsgList ("up to" because it will short-circuit when it does happen to match).

    0 讨论(0)
  • 2021-02-01 03:08

    You could do something like:

    HashSet<int> sentIDs = new HashSet<int>(SentList.Select(s => s.MsgID));
    
    var results = MsgList.Where(m => !sentIDs.Contains(m.MsgID));
    

    This will return all messages in MsgList which don't have a matching ID in SentList.

    0 讨论(0)
提交回复
热议问题