Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 转换时,我将LINQ转换为实体Int32到Int32(System.String)_Entity Framework_Linq To Sql - Fatal编程技术网

Entity framework 转换时,我将LINQ转换为实体Int32到Int32(System.String)

Entity framework 转换时,我将LINQ转换为实体Int32到Int32(System.String),entity-framework,linq-to-sql,Entity Framework,Linq To Sql,转换时,我将LINQ转换为实体Int32到Int32(System.String)。我尝试了int.Parse()、SqlFunction和EdmFunction,但问题仍然存在 例外情况: System.NotSupportedException: LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated i

转换时,我将LINQ转换为实体Int32到Int32(System.String)。我尝试了int.Parse()、SqlFunction和EdmFunction,但问题仍然存在

例外情况:

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
代码:


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

或者,您必须提取要查找的变量,将其转换为C#,然后在另一个查询中将其用作变量。或者,如果您有权访问数据库,则可以将两个CategoryID都设置为Int。类似于这些字段的类似字段应该是同一类型,这是有道理的


希望有帮助

我建议将c.CategoryId转换为如下字符串

 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
             };
var query=来自me.Products中的p
从me.ProductCategories中的c开始
让categoryId=me.ProductCategories.Take(1).选择(x=>c.categoryId).Cast().FirstOrDefault()
其中p.CategoryId==CategoryId
选择新的
{
p、 产品名称,
c、 类别名称
};

为什么类别在一个位置存储为字符串,在其他位置存储为整数?对我来说,看起来像是一种代码味道,你可以在p.CategoryId上更改为-->
等于c.CategoryId.ToString()
假设p.CategoryId是stringtype而c.CategoryId是int32type我知道你可以在Linqpad中将.ToInt32(p.CategoryId)转换为Linq查询,我知道一个事实,我无法理解Linqpad背后的天才不遵守LINQ/C规则和api。我在linqpad中转换了.toint32,但随后我将.net c#应用程序的其他值改为.ToString()。基本上,我是说Convert.ToInt32的特定C#操作与我所知道的一样好。
 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
             };