2013-02-12 81 views
1

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

如果说明已经是字符串类型了,为什么你需要在它上面调用ToString()? – 2013-02-12 13:49:50

+0

@WiktorZychla,可能是他不需要名称和描述,但他需要日期以字符串...'(q.Year.ToString(“MMM ddd d HH:mm yyyy”))' – 2013-02-12 13:54:35

+0

可能重复[为什么实体框架无法在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

回答

1

先拿到名单查询,并选择新的类型为ProductDTO

var productDTO = (from q in productResults 
       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(); 

COMMENT

IEnumerable的AFTER:

IEnumerable<Products> list = context.Products.Take(10); 
// after this line data load the memory that fetched from DB. 

SQL输出:

Select * FROM Table 

IQuerable:

IQuerable<Products> list = context.Products.Take(10); 
// data still not fetch from DB 

SQL输出:

Select Top 10 FROM Table 
+0

谢谢.. !! ..我会试一试.. – Ren 2013-02-12 14:04:39

+0

It works..Thanks much .. !!。如果可能,请在下面的答案中看到我的其他问题,并让我知道你的想法。谢谢 – Ren 2013-02-12 14:18:39

1

你也可以做到这一点的一个查询;

  var productResults = ctx.Products.Where(q => q.ProductId == productId && q.Model == productModel).ToList() 
       .Select<Product, ProductDTO>(m => 
       { 
        Models.ProductDTO dto= new Models.ProductDTO(); 
        dto.Id = m.ProductId; 
        dto.Name = m.Name.ToString(); 
        dto.Year = m.Year.ToString("MMM ddd d HH:mm yyyy"); 
        dto.Model = m.Model; 
        dto.Description = m.Description.ToString(); 
        return dto; 
       }).Distinct().ToList().AsParallel(); 

可能有更好的办法,但将其分成两个查询可能会有效。

var productResults = (from q in ctx.Products 
    where q.ProductId == productId && q.Model == productModel 
    select q).ToList(); 

var temp = from o in productResults 
     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(); 
+0

谢谢。我会试一下。!! – Ren 2013-02-12 14:02:43

+0

这里的技巧是在第一次查询后调用列表;那么它的工作方式与任何Linq查询的工作方式完全相同,因为数据被提取到内存中。 – daryal 2013-02-12 14:04:11

+0

更好的方法是使用'.AsEnumerable()'而不是第一个'.ToList()':如果要放弃该列表,那么将所有'Product'保存在列表中没有意义。唯一使用'.ToList()'的办法是强制'.Select()'解析为'Enumerable.Select'而不是'Queryable.Select','.AsEnumerable()'实现了同样的结果。 – hvd 2013-02-12 14:07:40

相关问题