我無法編譯下面的代碼,我發現了以下錯誤:EF-linq2entities不能識別.tostring()方法?
LINQ to Entities does not recognize the method 'System.String ToString()
我有點驚訝,因爲我能做到這一點在LINQ2SQL,但不能做到這一點在實體框架。
請問我可以幫助重寫下面的代碼嗎?我已經看到了一些與這個錯誤有關的例子,但是我找不到特定於這個場景的東西。從context
var productResults = ctx.Products.where(q => q.ProductId == productId && q.Model == productModel).ToList();
然後感謝
using (ctx)
{
var productResults = (from q in ctx.Products
where q.ProductId == productId && q.Model == productModel
select new Models.ProductDTO
{
Id = q.ProductId,
Name = q.Name.ToString(),
Year = q.Year.ToString("MMM ddd d HH:mm yyyy"),
Model = q.Model,
Description = q.Description.ToString()
}).Distinct().ToList().AsParallel();
Department.Products = productResults;
}
如果說明已經是字符串類型了,爲什麼你需要在它上面調用ToString()? – 2013-02-12 13:49:50
@WiktorZychla,可能是他不需要名稱和描述,但他需要日期以字符串...'(q.Year.ToString(「MMM ddd d HH:mm yyyy」))' – 2013-02-12 13:54:35
可能重複[爲什麼實體框架無法在LINQ語句中使用ToString()?](http://stackoverflow.com/questions/1920775/why-would-entity-framework-not-be-able-to-use-tostring -in-a-linq-statement) – Matt 2015-03-06 09:13:27