我写了一个linq语句来获取在提供teacher_Id之后按courseId分组的student_ID列表:
foreach (var item4 in TeacherName)
{
var studentInthisClassId = from s in DB.ClassMembers
where s.Teacher_Id == item4.Key
group s.Student_Id by s.CourseId
into thisClass
select new {Id = thisClass.Key, sid = thisClass.ToList()};
我写了一个循环来使用上面的查询,但它跳出来,甚至没有运行循环一次。这是循环的代码:
int j = 0;
foreach (var studentId in studentInthisClassId)
{
string sfName =
DB.Students.Where(n => n.Student_Id == studentId.sid[j]).Select(p => p.StudentName).First();
string slName =
DB.Students.Where(n => n.Student_Id == studentId.sid[j])
.Select(p => p.StudentLastName)
.First();
string studentsFulName = string.Format("{0},{1}", sfName, slName);
StudentName.Add(studentsFulName);
if (!StudentsByTeacherName.ContainsKey(item4.Value))
{
StudentsByTeacherName.Add(item4.Value, StudentName);
}
j++;
}
这是来自linq的答案:
studentInthisClassId = {SELECT
[Project3].[C1] AS [C1],
[Project3].[CourseId] AS [CourseId],
[Project3].[C2] AS [C2],
[Project3].[Student_Id] AS [Student_Id]
FROM ( SELECT
[Distinct1].[CourseId] AS [CourseId],
1 AS [C1],
...