4

The below exception flown

LINQ to Entities does not recognize the method 'Boolean Contains(Int32)' method, and this method cannot be translated into a store expression.

while trying to execute the below query

List<int> studentIDs = Common.getFilterStudents();
var query = from a in studentTable
            where studentIDs.Contains(a.StudentID)
            select a;

How can I filter the query using the studentIDs list?

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
User2012384
  • 4,769
  • 16
  • 70
  • 106

1 Answers1

6

It is not possible to use Contains in your EF version, because support for Contains was added in EF 4. Either upgrade your EF version (then your code will work without any problems), or use something like manual expression building:

var query = context.studentTable.Where(
    BuildContainsExpression<Student, int>(s => s.StudentID, studentIDs));
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459