这些项目我会假设有一个简单的LINQ查询来做到这一点,我只是不完全知道如何。请参考下面代码段,注释所描述的,我想怎样做:使用LINQ获取只有列表<>
我有三个表
Course
Id Name
---- ------
1 php
2 joomla
3 mysql
MainCourse
Id CourseCode
---- -------------
1 Foundation
2 Diploma
3 Professional
SubCourse
Id CourseId MainCourseId
---- --------- --------------
1 1 1
2 1 2
3 2 2
4 1 3
5 2 3
6 3 3
List<int> courseIdList={1,2}
//I would like to perform a LINQ query to give me 'CourseCode'
//from 'courseIdList'
//this example should give me 'Diploma'
//CourseIdList could vary below are the expected results
//CourseIdList={1} ==> CourseCode=Foundation(MainCourse)
//CourseIdList={1,2} ==> CourseCode=Diploma(MainCourse)
//CourseIdList={1,2,3} ==> CourseCode=Professional(MainCourse)
//Rule is that I want to get only those items from the subcourse
//where 'courseId(subcourse)' matches the 'courseIdList'
//Say CourseIdList={1,2},I need to omit the following Id (1,4,5,6)
//from subcourse since MainCourseId=1 , contains courseId =1 (not 1,2)
//and MainCourseId=3 , contains courseId = 1,2,3 (not 1,2)
这不是很清楚。为什么只有ID 2和3? “CourseId”1或2的其他人怎么样? –
当你说LINQ时,你在查询内存中的对象吗?或通过ORM的数据库?要么...? (LINQ有很多不同的实现) –
'courseIdList'中的项目与'SubCourse'中的项目匹配?你在'MainCourseId'上匹配吗?否则这根据您的预期输出+输入是没有意义的。好吧,你现在编辑了你的问题,并没有提到'SubCourse'?无论如何,为什么预期产出现在只是'文凭'而不是'基础'和'文凭'(1,2)? – DGibbs