C# 如何使用Linq to SQL的本地集合?

C# 如何使用Linq to SQL的本地集合?,c#,linq-to-sql,collections,.net-4.0,notsupportedexception,C#,Linq To Sql,Collections,.net 4.0,Notsupportedexception,我读到了和我一样的问题。不幸的是,标记的解决方案没有帮助。我可能误解了LINQ的一些非常明显的事情 我正在尝试进行排序的反向查找。我需要找到学生注册的所有课程 这是密码 public static IQueryable GetCoursesByStudent(string sStudentId) { Ld_Sql_ServerDataContext ld_SqlContext = new Ld_Sql_ServerDataContext(); // course-lesson

我读到了和我一样的问题。不幸的是,标记的解决方案没有帮助。我可能误解了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:不支持使用本地集合的查询


有人知道发生了什么吗

我认为您可以进行两个linq连接,只需一个查询就可以完成这项工作,就像在SQL上一样。所以前两个查询可以消失。

你能试试这个吗

我认为您应该在使用前将ActivityEnrollmentId和ParentId转换为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;
    }

那是因为Linq的懒惰评估这正是Anthony的建议。我试过了,但没用。后来我发现这是因为我运行了两个类似的查询,而第二个查询也需要修复。对不起,安东尼!类似这样的事情:我不知道该数据结构是否有效,因为LinQ查询可能很棘手。但我认为您可以这样做:var courses=from c in ld_SqlContext.YT_TRAINING_COMPONENTs在ce上加入ld_SqlContext.YT_STUDENT_COURSE_registration_STATUS。STUDENT_EMPLOYEE_ID_NR.ToLower()等于ssstudentid.ToLower()。。。等正如我所说的,在LinQ上进行连接可能很棘手,但它可能会起作用。