在C#中的对象列表中使用RemoveAll而不删除项目

时间:2016-03-20 05:46:57

标签: c# linq

我有一个消息列表。我想通过列表并执行以下操作:

  1. 如果SenderID = 100,则将ShowInSenderInbox和ShowInSenderOutbox值设置为Zero。
  2. 如果ReceiverID = 100,则将ShowInReceiverInbox值设置为零。 一旦满足上述两个条件,我想检查以下内容:
  3. 如果ShowInSenderInbox == 0&& ShowInSenderOutbox == 0&& ShowInReceiverInbox == 0                            然后从列表中删除此项目。
  4. 目前,我的代码将ShowInSenderInbox,ShowInSenderOutbox和ShowInReceiverInbox的值设置为零。但是,它不会从列表中删除与条件3匹配的项目。

    以下是完整代码:

    public class Message
    {
       public int MessageID { get; set; }
       public int SenderID { get; set; }
       public int ReceiverID { get; set; }
       public int ShowInSenderInbox { get; set; }
       public int ShowInSenderOutbox { get; set; }
       public int ShowInReceiverInbox { get; set; }
    }
    
    public bool GetItem(Message I)
    {           
       if (I.SenderID == 100)
       {
          I.ShowInSenderInbox = 0;
          I.ShowInSenderOutbox = 0;
       }
       else if (I.ReceiverID == 100)
       {
          I.ShowInReceiverInbox = 0;
       }
    
       if (I.ShowInSenderInbox == 0 && I.ShowInSenderOutbox == 0 && I.ShowInReceiverInbox == 0)
       {
          return true;
       }
       else
       {
          return false;
       }                             
    }
    
    
    List<Message> messages = new List<Message>() {
        new Message { MessageID=1, SenderID=100, ReceiverID=200, ShowInSenderInbox=1, ShowInSenderOutbox=1, ShowInReceiverInbox=1 },
        new Message { MessageID=2, SenderID=101, ReceiverID=300, ShowInSenderInbox=1, ShowInSenderOutbox=1, ShowInReceiverInbox=1 },
        new Message { MessageID=3, SenderID=102, ReceiverID=100, ShowInSenderInbox=1, ShowInSenderOutbox=0, ShowInReceiverInbox=0 },
        new Message { MessageID=4, SenderID=100, ReceiverID=101, ShowInSenderInbox=0, ShowInSenderOutbox=1, ShowInReceiverInbox=0 },
        new Message { MessageID=5, SenderID=200, ReceiverID=300, ShowInSenderInbox=1, ShowInSenderOutbox=0, ShowInReceiverInbox=0 },
        new Message { MessageID=9, SenderID=100, ReceiverID=400, ShowInSenderInbox=1, ShowInSenderOutbox=1, ShowInReceiverInbox=1 },
        new Message { MessageID=11, SenderID=400, ReceiverID=100, ShowInSenderInbox=1, ShowInSenderOutbox=1, ShowInReceiverInbox=0 },
        new Message { MessageID=21, SenderID=80, ReceiverID=150, ShowInSenderInbox=1, ShowInSenderOutbox=0, ShowInReceiverInbox=1 },
        new Message { MessageID=23, SenderID=100, ReceiverID=300, ShowInSenderInbox=1, ShowInSenderOutbox=1, ShowInReceiverInbox=0 },
        new Message { MessageID=28, SenderID=50, ReceiverID=90, ShowInSenderInbox=1, ShowInSenderOutbox=0, ShowInReceiverInbox=1 },
        new Message { MessageID=29, SenderID=100, ReceiverID=900, ShowInSenderInbox=1, ShowInSenderOutbox=1, ShowInReceiverInbox=1 },
        new Message { MessageID=22, SenderID=500, ReceiverID=900, ShowInSenderInbox=0, ShowInSenderOutbox=0, ShowInReceiverInbox=0 },
        new Message { MessageID=39, SenderID=409, ReceiverID=500, ShowInSenderInbox=0, ShowInSenderOutbox=0, ShowInReceiverInbox=0 },
        new Message { MessageID=41, SenderID=105, ReceiverID=900, ShowInSenderInbox=0, ShowInSenderOutbox=0, ShowInReceiverInbox=0 }
     };
    
    ( from item in messages
      where item.SenderID == 100 || item.ReceiverID == 100
      select item )
                   .ToList()
                   .RemoveAll( I => GetItem(I) );
    

    http://www.geocities.ws/sonnyk2015/dosscreen2.png

1 个答案:

答案 0 :(得分:0)

只需添加此行:

messages.RemoveAll(i => i.ShowInSenderInbox == 0 && i.ShowInSenderOutbox == 0 && i.ShowInReceiverInbox == 0);