LINQ to Entities无法识别方法'System.Collections.Generic.List`1 [System.Int32]

时间:2013-05-23 01:36:29

标签: linq entity-framework

我有以下错误:

        LINQ to Entities does not recognize the method 
'System.Collections.Generic.List`1 [System.Int32] get_st_past_enrollment_success()' 
method, and this method cannot be translated into a store expression.

这是由以下linq

引起的
IEnumerable<subject> _subjects = (from subject in context.subjects
                           where 
                                subject.enrollments.Count() < subject.sj_max_enrollment
                                && subject.sj_availability == true
                                && !this.get_st_past_enrollment_success().Contains(subject.sj_subject_id)

                           select subject);

get_st_past_enrollment_success()返回一个List:

public List<int> get_st_past_enrollment_success()
        {
            return this.enrollments.Where(e => e.em_enrolled == false && e.em_result >= 50).Select(e => e.em_subject_id).ToList();
        }

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

您的查询本身包含方法调用 - 实体框架不知道该怎么做。尝试在查询之前提取列表提取:

var enrollments = get_st_past_enrollment_success();

var _subjects = from subject in context.subjects
                where subject.enrollments.Count() < subject.sj_max_enrollment
                      && subject.sj_availability
                      && !enrollments.Contains(subject.sj_subject_id)
                select subject;

另请注意,get_st_past_enrollment_success违反.NET naming conventions - 这不会影响代码是否有效,但对于习惯于常规约定的其他开发人员来说,这看起来很奇怪。