将字符串列表与另一个列表中的对象属性进行比较

Compare list of strings with object properties in another list

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

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

我刚才有一个工作表情,长话短说,我现在没有了,我的笔记本电脑和显示器不是一百万件真是个奇迹:)

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

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

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

你收到错误是因为 Where expects a boolean predicate but you are again passing Where which return IEnumarable<T> and thus the error. You need Any 这里将 return 匹配条件的布尔值:-

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