4

我读了这个问题,这与我遇到的问题相同。不幸的是,标记的解决方案没有帮助。我可能误解了关于 LINQ 的一些非常明显的事情。

我正在尝试进行某种反向查找。我需要找到学生注册的所有课程。

这是代码...

public static IQueryable GetCoursesByStudent(string sStudentId)
{
    Ld_Sql_ServerDataContext ld_SqlContext = new Ld_Sql_ServerDataContext();

    // course-lesson IDs
    var activityEnrollmentIds = from ce in ld_SqlContext.YT_STUDENT_COURSE_ENROLLMENT_STATUS
                                where ce.STUDENT_EMPLOYEE_ID_NR.ToLower() == sStudentId.ToLower()
                                select ce.TRAINING_ACTIVITY_ID;

    // lesson parent course IDs
    var parentIds = from c in ld_SqlContext.YT_TRAINING_COMPONENT_RLTNPs
                    where activityEnrollmentIds.Contains(c.TRAINING_ACTIVITY_ID)
                    select c.PARENT_TRAINING_ACTIVITY_ID;

    // filtered list of courses    
    var courses = from c in ld_SqlContext.YT_TRAINING_COMPONENTs
                    where c.TRAINING_ACTIVITY_TYPE_DC == "Course" && 
                        (activityEnrollmentIds.ToList().Contains(c.TRAINING_ACTIVITY_ID)
                            || parentIds.ToList().Contains(c.TRAINING_ACTIVITY_ID))
                    select c;

    return courses;
}

我将结果数据绑定到一个 ASP:ListBox 并在 DataBind() 上引发以下错误...

System.NotSupportedException:不支持使用本地集合的查询。

有人知道发生了什么吗?

4

2 回答 2

1

你能试试这个吗?

我认为您应该在使用之前将 activityEnrollmentIds 和 parentIds 转换为 List 。

    public static IQueryable GetCoursesByStudent(string sStudentId)
    {
        Ld_Sql_ServerDataContext ld_SqlContext = new Ld_Sql_ServerDataContext();

        // course-lesson IDs
        var activityEnrollmentIds = (from ce in ld_SqlContext.YT_STUDENT_COURSE_ENROLLMENT_STATUS
                                    where ce.STUDENT_EMPLOYEE_ID_NR.ToLower() == sStudentId.ToLower()
                                     select ce.TRAINING_ACTIVITY_ID).ToList();

        // lesson parent course IDs
        var parentIds = (from c in ld_SqlContext.YT_TRAINING_COMPONENT_RLTNPs
                        where activityEnrollmentIds.Contains(c.TRAINING_ACTIVITY_ID)
                         select c.PARENT_TRAINING_ACTIVITY_ID).ToList();

        // filtered list of courses    
        var courses = from c in ld_SqlContext.YT_TRAINING_COMPONENTs
                      where c.TRAINING_ACTIVITY_TYPE_DC == "Course" &&
                          (activityEnrollmentIds.Contains(c.TRAINING_ACTIVITY_ID)
                              || parentIds.Contains(c.TRAINING_ACTIVITY_ID))
                      select c;

        return courses;
    }
于 2012-01-05T01:55:33.880 回答
0

我认为你可以做两个 linq 连接,只用一个查询就可以完成工作,就像你在 SQL 上做的那样。所以前两个查询可以消失。

于 2012-01-05T01:33:55.430 回答