Search code examples
linqiqueryableentities

LINQ to Entities does not recognize the method 'System.String get_Item(System.String)' method


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

Solution

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