比较字符串列表和另一个列表中的对象属性 [英] Compare list of strings with object properties in another list

查看:38
本文介绍了比较字符串列表和另一个列表中的对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含课程代码的字符串列表,以及一个以 courseCode 作为属性的对象的列表.

I have a list of strings that contain course codes, and a list of objects who have courseCode as a property.

我试图找到一个linq表达式来比较两者,并让我知道字符串列表中的项目与对象列表中的courseCode属性之间是否存在任何匹配项.

I'm trying to find a linq expression to compare the two, and let me know if there are any matches, at all, between the items in the list of strings and the courseCode properties in the list of objects.

我刚才有一个有效的表达方式,长话短说,我不再了,这是我的笔记本电脑和显示器没有上百万个奇迹的奇迹:)

I had a working expression moments ago and, long story short, I do not anymore and it's a miracle my laptop and monitors aren't in a million pieces :)

下面是我目前在比较中的最佳猜测. results 是对象列表,而 coursesThatWork 是字符串列表.下面的表达式给出了错误消息

Below is my current best guess at the comparison. results is the list of objects while coursesThatWork is the list of strings. The expression below gives the error message

无法将表达式类型'System.Collections.Generic.IEnumerable转换为返回类型'bool'

Cannot convert expression type 'System.Collections.Generic.IEnumerable to return type 'bool'

results.Where(x => coursesThatWork.Where(y => y.Equals(x.CourseCode))).Count() == 0

推荐答案

由于出现任何返回匹配条件的布尔值:-

You are getting the error because Where expects a boolean predicate but you are again passing Where which return IEnumarable<T> and thus the error. You need Any here which will return a boolean for matching condition:-

results.Where(x => coursesThatWork.Any(y => y.Equals(x.CourseCode))).Count() == 0

这篇关于比较字符串列表和另一个列表中的对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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