首页 > 解决方案 > Linq To Entities - 在 DB 上运行 CONVERT to int

问题描述

我在 DB 中的表包含 varchar 字段,其中包含字符串和整数值。要求是仅将整数与提供的值进行比较。这段代码

from c in Cars
where SqlFunctions.IsNumeric(c.Code) 
      && Convert.ToInt32(c.Code) == 12

投掷

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

在 LINQPad 中运行此查询运行良好!为什么?

问题:如何直接在数据库中转换字符串并与提供的整数值进行比较。

标签: entity-frameworklinqlinq-to-sqllinq-to-entitieslinqpad

解决方案


为什么不只是:

from c in Cars
where c.Code == "12"

推荐阅读