簡體   English   中英

使用.Where()的C#實體框架過濾

[英]C# Entity Framework filtering with .Where()

我正在使用Entity Framework在C#中工作,並且嘗試過濾聯系人查詢以獲取具有相同ID的所有聯系人。 我可以獲取所有Contacts ,但是使用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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM