简体   繁体   中英

Compare list of strings with object properties in another list

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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