Monday, September 5, 2011

LINQ To Entity and Convert class Methods Problems

While i was working with linq to sql or entity framework i have getting error at runtime like:

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

and my code is:

                var lst = (from p in context.Products
                           where p.CategoryID==Convert.ToInt32(this.Request.QueryString["Category"])
                           select p).ToList();

Than i have found that:

Reason: First value of type "object" is returned and then it is typecasted to int. This is, again, not permitted as a temporary anonymous object needs to be created for resolving translation of Data Type.

Solution of this:

int id = Convert.ToInt32(this.Request.QueryString["Category"]);
                var lst = (from p in context.Products
                           where p.CategoryID==id
                           select p).ToList();




1 comment:

  1. Thank you, this article helps me to understand why I got this kind of error.

    ReplyDelete