Compare list of strings with object properties in another list

爷,独闯天下 提交于 2021-02-11 04:58:56

问题


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

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 :)

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

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

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

回答1:


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


来源:https://stackoverflow.com/questions/33553373/compare-list-of-strings-with-object-properties-in-another-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!