我无法编译下面的代码,我发现了以下错误: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