Asp.net-Mvc-3

無法將 lambda 表達式轉換為類型“字元串”,因為它不是委託類型

  • June 14, 2019

在我的控制器中,我嘗試使用包含 EF4 來選擇相關實體,但 lambda 表達式引發以下錯誤,

我在 Entity 類中定義了相關實體,例如

public class CustomerSite
{
   public int CustomerSiteId { get; set; }
   public int CustomerId { get; set; }
   public virtual Customer Customer { get; set; }
}

然後在我的控制器中我有

var sites = context.CustomerSites.Include(c => c.Customer);

public ViewResult List()
{
   var sites = context.CustomerSites.Include(c => c.Customer);
   return View(sites.ToList());
}

誰能指出我在這裡做錯了什麼的正確方向?

好吧,文章很舊,但只是在這裡回復以更新它。嗯,Entity Framework 4.1Include()的方法有擴展方法,它也接受一個 lambda 表達式。所以

context.CustomerSites.Include(c => c.Customer);

是完全有效的,你需要做的就是使用這個:

using System.Data.Entity;

Include 是 System.Data.Entity 命名空間中的擴展方法,需要添加:

using System.Data.Entity;

然後你可以使用 lambda 表達式,而不是字元串。

引用自:https://stackoverflow.com/questions/6696348