简体   繁体   English

使用.Where()的C#实体框架过滤

[英]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. 我正在使用Entity Framework在C#中工作,并且尝试过滤联系人查询以获取具有相同ID的所有联系人。 I can get all Contacts , but I'm having issues filtering using Where . 我可以获取所有Contacts ,但是使用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. 提供程序在识别无法转换为SQL的表达式时遇到问题。 Try to simplify the expressions so that it can be translated to SQL more easily. 尝试简化表达式,以便可以更轻松地将其转换为SQL。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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