2

我有以下错误:

        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() 返回一个列表:

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();
        }

我在这里做错了什么?

4

1 回答 1

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 命名约定- 这不会影响代码是否有效,但对于习惯于正常约定的其他开发人员来说,它会显得很奇怪。

于 2013-05-23T01:38:46.490 回答