Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why LINQ to Entities does not recognize the method 'System.String ToString()?

Getting error inside MVC3 web application. LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

when i try to fetch values using EF from query :

public class DataRepository     {         public mydataEntities1 dbContext = new mydataEntities1();          public List<SelectListItem> GetPricingSecurityID()         {         var pricingSecurityID = (from m in dbContext.Reporting_DailyNAV_Pricing                                      select new SelectListItem                                          {                                                 Text = m.PricingSecurityID.ToString(),                                                 Value = m.PricingSecurityID.ToString()                                          });          return pricingSecurityID.ToList();         }     } 
like image 515
Neo Avatar asked Apr 11 '12 16:04

Neo


People also ask

Can I use ToString in Linq?

String ToString(Int32)' You cannot use these conversion functions in a LINQ to Entities statement, they cannot be translated to SQL, you need to do the conversions in memory.

What happens if you call ToString on a string?

The toString() method returns a string as a string. The toString() method does not change the original string.

Is Linq deprecated?

No it is not.


1 Answers

That can't be converted to SQL. I guess, in theory, it could, but isn't implemented.

You just need to perform your projection after you have your results:

var pricingSecurityID = (from m in dbContext.Reporting_DailyNAV_Pricing                                      select m.PricingSecurityID).AsEnumerable()     .Select(x => new SelectListItem{ Text = x.ToString(), Value = x.ToString() }); 
like image 191
Jay Avatar answered Sep 20 '22 21:09

Jay