使用 .Where() 进行 C# Entity Framework 过滤

标签 c# entity-framework filtering

我正在使用 Entity Framework 在 C# 中工作,我正在尝试过滤联系人查询以获取具有相同 ID 的所有联系人。我可以获取所有联系人,但在使用Where进行过滤时遇到问题。我知道出了问题,但我无法准确指出,任何帮助将不胜感激。

参见下面的相关代码:

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

最佳答案

提供程序在识别表达式时遇到问题,无法将其转换为 SQL。尽量简化表达式,以便更容易翻译成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;
}

关于使用 .Where() 进行 C# Entity Framework 过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36574392/

相关文章:

c# - 从静态类访问 HttpContextBase 对象

c# - 这个语法在c#中是什么意思?

c# - EF6 Code First 在模型更改时删除表(不是整个数据库)

c# - 从 Entity Framework 返回自定义对象 <List T> 并分配给对象数据源

list - 如何在 Kotlin 中过滤 ArrayList,以便我只有符合我的条件的元素?

c# 字段永远不会分配给并且将始终具有其默认值 null?

c# - 为什么我的 C# AppDomain 前一秒还好,下一秒就抛出异常?

.net - 如何将 Entity Framework 从6降级到5?

laravel - 如何使用Eloquent过滤数据透视表?

django - 如何过滤django-taggit top标签