11

I've looked at the various solutions here but none of them seem to work for me, probably because I'm too new to all this and am groping in the dark a bit. In the code below, the object "appointment" contains some basic LDAP information. From a list of such objects I want to be able to get a single record, based on employee id. I hope the code here is sufficient to illustrate. FTR, I've tried various formulations, including trying to use from and a select. All fail with the error given in the Title above.

IQueryable<appointment> query = null;

foreach(var record in results)
{
    BoiseStateLdapDataObject record1 = record;
    query = db.appointments.Where(x => x.student_id == record1.Values["employeeid"]);
}

if (query != null)
{
    var selectedRecord = query.SingleOrDefault();
}
david.s
  • 11,283
  • 6
  • 50
  • 82
user2283231
  • 125
  • 1
  • 1
  • 6

1 Answers1

21

Try to move employee id getting out of query:

IQueryable<appointment> query = null;

foreach(var record in results)
{
    var employeeId = record.Values["employeeid"];
    query = db.appointments.Where(x => x.student_id == employeeId);
}

if (query != null)
{
    var selectedRecord = query.SingleOrDefault();
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Thanks. That fixed the error. Can you tell me *why* it fixed it? Or is that outside the scope of this forum? – user2283231 Apr 15 '13 at 19:08
  • 3
    @user2283231 When you are using Linq to Entities, expression tree is passed to `Where` method, then it's analyzed and converted to SQL. EF simply could not convert indexer call to SQL. That's why passing plain value fixed the problem. Feel free to accept answer if it was helpful :) – Sergey Berezovskiy Apr 15 '13 at 19:23