2013-02-12 70 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

相關問題