转换时,我获得LINQ to Entities Int32 ToInt32(System.String)

Pus*_*kin 2 entity-framework linq-to-sql

当convert.i尝试int.Parse(),SqlFunction和EdmFunction时,我得到LINQ to Entities Int32 ToInt32(System.String),但问题仍在继续.

例外:

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
Run Code Online (Sandbox Code Playgroud)

码:

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);
    }
Run Code Online (Sandbox Code Playgroud)

Log*_*ard 9

您不能在linq查询中使用Convert.ToInt32.Linq有自己的语法,不识别外部方法.

您必须将要查找的变量提取到C#,转换它,并将其用作另一个查询中的变量.或者,如果您有权访问数据库,则可以同时创建categoryIDs.有意义的是,像那些类似的字段应该是相同的类型.

希望有所帮助!