[英]Converting a string to number DB2 Entity Framework
I'm migrating a legacy DB2 Application to a web application and I'm using ASP.NET WebAPI with entity framework. 我正在将旧版DB2应用程序迁移到Web应用程序,并且将ASP.NET WebAPI与实体框架一起使用。
This is the first time I'm working with DB2. 这是我第一次使用DB2。
I have installed the necessary drivers and fix packs for DB2 database.I have generated the EDMX for existing DB2 database tables. 我已经为DB2数据库安装了必要的驱动程序和修订包。为现有的DB2数据库表生成了EDMX。
Everything works fine. 一切正常。 I have encountered a strange issue that, In one of the DB2 SQL query, a string column is compared with a number value. 我遇到了一个奇怪的问题,在一个DB2 SQL查询中,将字符串列与数字值进行比较。
Consider the below query 考虑以下查询
SELECT DISTINCT DB23.EMPLOYEES.FIRST_NAME,
DB23.EMPLOYEES.LAST_NAME,
STRIP("DB23"."EMPLOYEES"."STATE_NAME") STATE_NAME,
DB23.CONTACT.PHONE_NO
FROM DB23.EMPLOYEES,
DB23.CONTACT
WHERE ( DB23.EMPLOYEES.EMP_ID = DB23.CONTACT.EMP_ID ) and
( DB23.CONTACT.EMP_REG_NO < '900000' )
ORDER BY DB23.EMPLOYEES.FIRST_NAME ASC
here the column EMP_REG_NO is a string type and it is compared with a number using less than ' < ' operator and the strange thing is that the same query runs perfectly in IBM DB2 Server Client. 这里的EMP_REG_NO列是一个字符串类型,并且使用小于' < '运算符将其与数字进行比较,奇怪的是,同一查询在IBM DB2 Server Client中可以完美运行。
The below one is the Linq expression that I have written 下面的是我写的Linq表达式
from emp in _DB2Context.EMPLOYEES
join cont in _DB2Context.CONTACT on emp.EMP_ID equals cont.EMP_ID
where cont.EMP_REG_ID < 900000
select new {
emp.FIRST_NAME
emp.LAST_NAME
emp.STATE_NAME
cont.PHONE_NO
};
Here we cannot compare the number 900000 with string type column. 在这里,我们无法将数字900000与字符串类型列进行比较。
I have tried like this 我已经尝试过这样
from emp in _DB2Context.EMPLOYEES
join cont in _DB2Context.CONTACT on emp.EMP_ID equals cont.EMP_ID
where Convert.ToInt64(cont.EMP_REG_ID) < 900000
select new {
emp.FIRST_NAME
emp.LAST_NAME
emp.STATE_NAME
cont.PHONE_NO
};
This gave me an error that LINQ doesn't recognize method Convert.ToInt64() and I understood that There is no equivalent method to achieve the conversion in T-SQL statement. 这给了我一个错误,即LINQ无法识别方法Convert.ToInt64(),并且我知道在T-SQL语句中没有等效的方法来实现转换。
Any Ideas? 有任何想法吗?
Update: 更新:
The below is the table structure 下面是表格结构
TABLE - EMPLOYEES
***************************************************************************
Name Data Type Type schema Length Scale Nulls Hidden
--------------- ------------- ------------- -------- ------- ----- --------
EMP_ID CHARACTER SYSIBM 2 0 N Not
FIRST_NAME CHARACTER SYSIBM 30 0 N Not
LAST_NAME CHARACTER SYSIBM 30 0 N Not
STATE_NAME CHARACTER SYSIBM 25 0 N Not
FIPS_CTRY_CD CHARACTER SYSIBM 2 0 N Not
POLK_STATE_CD CHARACTER SYSIBM 2 0 N Not
MAIL_STATE CHARACTER SYSIBM 15 0 N Not
****************************************************************************
TABLE - CONTACT
****************************************************************************
Name Data Type Type schema Length Scale Nulls Hidden
--------------- ------------- ------------- -------- ------- ----- --------
EMP_ID CHARACTER SYSIBM 2 0 N Not
REG_NO CHARACTER SYSIBM 6 0 N Not
REG_STATE_CD CHARACTER SYSIBM 2 0 Y Not
PHONE_NO CHARACTER SYSIBM 15 0 N Not
REG_STATE CHARACTER SYSIBM 2 0 N Not
CORP_NAME CHARACTER SYSIBM 30 0 N Not
***************************************************************************
The below is the POCO classes generated by the entity framework 以下是实体框架生成的POCO类
public partial class EMPLOYEES
{
public string EMP_ID { get; set; }
public string FIRST_NAME { get; set; }
public string LAST_NAME { get; set; }
public string STATE_NAME { get; set; }
public string FIPS_CTRY_CD { get; set; }
public string POLK_STATE_CD { get; set; }
public string MAIL_STATE { get; set; }
}
public partial class CONTACT
{
public string EMP_ID { get; set; }
public string REG_NO { get; set; }
public string REG_STATE_CD { get; set; }
public string PHONE_NO { get; set; }
public string REG_STATE { get; set; }
public string CORP_NAME { get; set; }
}
moreover, I can able to run the below simple query in DB2 command line processor 此外,我可以在DB2命令行处理器中运行以下简单查询
SELECT
DB23.CONTACT.EMP_ID
FROM DB23.CONTACT
WHERE DB23.CONTACT.EMP_ID < '900000'
SELECT
DB23.CONTACT.EMP_ID
FROM DB23.CONTACT
WHERE DB23.CONTACT.EMP_ID < 900000
Both DB2 Query returns result 两个DB2查询都返回结果
EMP_ID
------
123
234
But I cannot do the same with LINQ like, 但是我不能像LINQ那样做,
from cont in _DB2Context.CONTACT
where cont.DLR_NO < 90000 // here getting an error says that Operator '<' cannot be applied to operands of type 'string' and 'int'
select new {
cont.DLR_NO
}
from cont in _DB2Context.CONTACT
where cont.DLR_NO < '90000' // here getting an error says that Operator '<' cannot be applied to operands of type 'string' and 'char'
select new {
cont.DLR_NO
}
Thanks, Gowrish. 谢谢,Gowrish。
Assuming that it will be translated into its SQL equivalent, try using something like 假设它将被转换为等效的SQL,请尝试使用类似
where cont.DLR_NO.CompareTo("90000") < 0
As for string to integer conversion, you have no guarantee that a CHARACTER
column in the database will only ever contain digits, which makes any program that insists on casting that column to an integer a risky bet. 至于字符串到整数的转换,您不能保证数据库中的CHARACTER
列将只包含数字,这使得任何坚持将该列转换为整数的程序都是危险的选择。 In those situations, it's safer to convert your numeric literal to a string first (from 90000
to "90000"
) so you're simply comparing two strings. 在这种情况下,先将数字文字转换为字符串(从90000
到"90000"
)比较安全,因此您只需要比较两个字符串即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.