我使用下面的代碼,並試圖內使用命名方法LINQ到實體如何使用命名方法
Customer FindCustomerByLastName(string lastname)
{
DataContext MyContext = new DataContext();
return MyContext.Customers.Where(c => GetByCustomerByLastName(c,lastname) == true).FirstOrDefault();
}
bool GetByCustomerByLastName(Customer Cust, string LastName)
{
if (Cust.LastName == LastName)
return true;
else
return false;
}
但是,這讓我在之後的運行時間錯誤。
LINQ實體無法識別方法「布爾 GetByCustomerByLastName(客戶,System.String)」方法,和這種方法 不能被翻譯成表達商店。
如果我改變我的方法使用lambda如下,一切工作正常。
Customer FindCustomerByLastName(string lastname)
{
DataContext MyContext = new DataContext();
return MyContext.Customers.Where(c => c.LastName == lastname).FirstOrDefault();
}
是否有可能在上下文中我想要做的Linq-To-Entities中使用命名方法?
你需要建立一個表達式,但這不是那麼簡單。看看這裏:http://msdn.microsoft.com/en-us/library/bb882637.aspx –