简体   繁体   English

从IList中删除项目 <Log> IList中存在log.id的位置 <short>

[英]Remove items from an IList<Log> where log.id exists in IList<short>

Hi i am new to linq kindly pardon my bad keywords. 嗨,我是linq的新手,请原谅我的不好的关键词。

 IList<Log> pendingLogs = rep.GetAllPending();
 IList<Short> pendingMessageInQueue = PendingCommandModel.GetPendingMessagesIds();

I have two lists. 我有两个清单。 I want to remove items from pendingLogs where item.Id exists in pendingMessageInQueue . 我想从pendingLogs中存在pendingMessageInQueue pendingLogs中删除项目。 I could also have used IEnumerable if that is more appropriate in this case. 如果在这种情况下更合适,我也可以使用IEnumerable

If you want to keep your original list instead of creating a new one , you can use a simple join to get the items you want to delete: 如果要保留原始列表而不是创建新列表,则可以使用简单的联接来获取要删除的项目:

 foreach(var log in pendingMessageInQueue.Join(pendingLogs, 
                                               (id) => id,
                                               (log) => log.Id,
                                               (id, log) => log))
{
    pendingLogs.Remove(log);
}

Create a new list with all items that you want to keep, you can use !Contains : 创建一个包含所有要保留的项目的新列表,可以使用!Contains

pendingLogs = pendingLogs 
    .Where(pl => !pendingMessageInQueue.Contains(pl.id))
    .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM