比较两个列表并使用linq返回不匹配的项目 [英] compare two list and return not matching items using linq

查看:56
本文介绍了比较两个列表并使用linq返回不匹配的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个清单

List<Sent> SentList;
List<Messages> MsgList;

两者都具有称为MsgID的相同属性;

both have the same property called MsgID;

MsgList            SentList  

MsgID Content      MsgID Content Stauts
1       aaa        1       aaa     0
2       bbb        3       ccc     0
3       ccc        
4       ddd
5       eee

我想将Msglist中的MsgID与已发送列表进行比较,并需要使用linq不在已发送列表中的项目

i want to compare the MsgID in Msglist with the sentlist and need items which are not in the sent list using linq

Result 

MsgID Content
2       bbb
4       ddd
5       eee

推荐答案

您可以执行以下操作:

HashSet<int> sentIDs = new HashSet<int>(SentList.Select(s => s.MsgID));

var results = MsgList.Where(m => !sentIDs.Contains(m.MsgID));

这将返回MsgList中所有在SentList中没有匹配ID的消息.

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

这篇关于比较两个列表并使用linq返回不匹配的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆