简体   繁体   中英

Create New List From Existing Lists Comparison

I have two lists:

List<Course> studentCourses = Course.GetCourses(student.PIDM, GetTermCode());  
List<OfferedCourse> offeredCourses = OfferedCourse.GetOfferedCourses();

studentCourses have the properties SubjectCode and CourseNumber .
offeredCourses also have the properties SubjectCode and CourseNumber .

I want to create a third list of type List<Course> that contains only items from studentCourses that match both the SubjectCode and CourseNumber of an item in offeredCourses .

Any help would be greatly appreciated.

Linq为此使用了非直观的语法:use Contains可能包含您所追求的值的集合中的Contains

var result = studentCourses.Where(c => offeredCourses.Contains(o => o.SubjectCode == c.SubjectCode && o.CourseNumber == c.CourseNumber));

The solution ended up being:

IEnumerable<Course> courses = studentCourses.Where(sc => offeredCourses.Any(oc => oc.SubjectCode == sc.SubjectCode && oc.CourseNumber == sc.CourseNumber));

A big thank you to cosmo0 who pointed me in the right direction and Magnus who double checked my work!

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