Ask your technical questions on forums or here :
ASP.NET or MVC | C# | Windows Phone
Microsoft Technology Journals by Abhimanyu K Vatsa
HOME ABOUT RAZOR BOOK SPEAKING MVC ASP.NET JQUERY VIDEOS ARCHIVE CONTACT

17 May 2015

LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression

LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression

First of all look at the screenshot of the error page


You cannot use Convert.ToInt32 inside your LINQ query. Linq has its own syntax and does not recognize external methods.

You have to extract the variable you are looking for to C#, convert it, and use it as a variable in another query.

The original code which was causing error is:

if (!string.IsNullOrWhiteSpace(customerID))
{
    payments = payments.Where(i => i.Invoice.CustomerID == Convert.ToInt32(customerID));
}

In above you can see customerID is string within if statement. This customerID is beging used inside LINQ query by converting ToInt32 inside LINQ query which is not permitted.

Now to fix this, all you need is convert ToInt32 outside LINQ query as give below

if (!string.IsNullOrWhiteSpace(customerID))
{
    int CustomerID = Convert.ToInt32(customerID);
    payments = payments.Where(i => i.Invoice.CustomerID == CustomerID);
}

Now LINQ query will not raise any complain.

Hope this helps.

Comment using Google Services (1 comment):