When I convert, I get LINQ to Entities Int32 ToInt32(System.String).

entity-framework linq-to-sql

Question

When I convert, I get LINQ to Entities Int32 ToInt32(System.String). I've tried using int.Parse(), SqlFunction, and EdmFunction, but the issue persists.

Exception:

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

Code:

try
    {
        ModelEntities me = new ModelEntities();
        var query = from p in me.Products
                    join c in me.ProductCategories
                    on Convert.ToInt32(p.CategoryId) equals c.CategoryId
                    select new
                    {
                        p.ProductTitle,
                        c.CategoryName
                    };
        rptProducts.DataSource = query;
        rptProducts.DataBind();
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
1
2
5/24/2012 1:41:00 PM

Popular Answer

Convert.ToInt32 is ineligible for usage inside linq queries. Linq has its own syntax and does not accept methods from other sources.

Either extract the desired variable to C#, convert it, and use it as a variable in a different query. If you have access to the database, you may also convert both categoryIDs to ints. It makes logical for those fields to be of the same type as they are comparable.

Hope that was useful.

9
5/24/2012 1:48:42 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow