转换时,我得到LINQ to Entity Int32 ToInt32(System.String) [英] I get LINQ to Entities Int32 ToInt32(System.String) when convert

查看:223
本文介绍了转换时,我得到LINQ to Entity Int32 ToInt32(System.String)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I get LINQ to Entities Int32 ToInt32(System.String) when convert.i tried int.Parse() , SqlFunction and EdmFunction but the problem still going on.

例外:

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

代码:

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);
    }


推荐答案

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

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

您必须将要查找的变量提取到C#,将其转换为变量在另一个查询。或者,如果您有权访问数据库,则可以使两个categoryID都为int。这样的类似字段应该是相同的类型是有道理的。

Either you have to extract the variable you are looking for to C#, convert it, and use it as a variable in another query. Or you could make both categoryIDs ints, if you have access to the database. It makes sense that similar fields like those should be of the same type.

希望有帮助!

这篇关于转换时,我得到LINQ to Entity Int32 ToInt32(System.String)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆