简体   繁体   中英

C# Entity Framework filtering with .Where()

I am working in C# using Entity Framework and I am trying to filter a query of contacts to get all contact that have the same Id. I can get all Contacts , but I'm having issues filtering using Where . I know somethings wrong but I can't quite pinpoint it, any help would be appreciated.

See relevant code below:

public IEnumerable<model.Contact> Execute(GetContactById parameters)
{
    IEnumerable<model.Contact> ContactsById = null;

    DbRetryHandler.RetryHandler(delegate(DeviceModelContext retryContext)
    {
        ContactsById = retryContext.Contact
                    .Where(c => c.Id.equals(parameters.Id))
                    .Select(c => new model.Contact
                     {
                         // unrelated code
                     });
                });

                return ContactsById;
}

The provider has issues recognizing expressions it cannot translate to SQL. Try to simplify the expressions so that it can be translated to SQL more easily.

public IEnumerable<model.Contact> Execute(GetContactById parameters)
{
     IEnumerable<model.Contact> ContactsById = null;
     DbRetryHandler.RetryHandler(delegate(DeviceModelContext retryContext)
     {
         var parametersId = parameters.Id; // <-- store id in variable
         camerasByDeviceId = retryContext.Contact
           .Where(c => c.Id == parametersId) // <-- use == instead of Equals
           .Select(c => new model.Camera
           {
               // unrelated code
           });
     });

     return ContactsById;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM