English 中文(简体)
转换时,我得到LINQ 到实体 Int32 ToInt32( 系统Stestem. string)
原标题:I get LINQ to Entities Int32 ToInt32(System.String) when convert

当转换( I) 尝试 Int. Parse () 、 SqlFunction 和 EdmFunction 时, 我将 LINQ 转到实体 Int32 ToInt32 (System. String) 上, 但问题仍然存在 。

例外:

System.NotSupported例外: 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);
    }
问题回答

您无法在 Linq 查询中使用 Translate. ToInt32。 Linq 有自己的语法, 并且不承认外部方法 。

要么您必须提取您正在寻找的变量到 C#, 转换为 C#, 并在另一个查询中使用它作为变量 。 或者, 如果您可以访问数据库, 您也可以同时使用“ 分类ID 英寸 ” 。 这样类似的字段应该具有相同的类型, 这样做是有道理的 。

希望有帮助!

我建议把C. C. 类Id 转换成这样字符串

 var query = from p in me.Products
             from c in me.ProductCategories
             let categoryId = me.ProductCategories.Take(1).Select(x => c.CategoryId).Cast<string>().FirstOrDefault()
             where p.CategoryId == categoryId 
             select new
             {
               p.ProductTitle,
               c.CategoryName
             };




相关问题
LINQ to SQL as databinding source for WPF Treeview

I wonder if someone could provide a simple example of the following. (preferably in VB.Net): I have an SQL database with related tables and I am successfully using LINQ to SQL in other areas of my ...

Linq to SQL insert/select foreign/primary records

I have table A and Table B. Table B contains two columns, Name and ID. Table A contains several columns and a foreign key column pointing to B.ID called B_ID On the client side, I have all the data I ...

LINQ to SQL Dynamic Sort question

How do I code the Select clause in my LINQ satament to select column aliases so I can sort on them basically I want to accomplish this SQL statement in LINQ: select type_id as id, ...

Using a schema other than dbo in LinqToSql Designer

Is there a way to specify in a data connection or the LinqToSql Designer a schema? Whenever I go to setup a data connection for LinqToSql there doesn t seem to be anyway to specify a schema and I ...

热门标签