[英]Converting a string to number DB2 Entity Framework
我正在将旧版DB2应用程序迁移到Web应用程序,并且将ASP.NET WebAPI与实体框架一起使用。
这是我第一次使用DB2。
我已经为DB2数据库安装了必要的驱动程序和修订包。为现有的DB2数据库表生成了EDMX。
一切正常。 我遇到了一个奇怪的问题,在一个DB2 SQL查询中,将字符串列与数字值进行比较。
考虑以下查询
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
这里的EMP_REG_NO列是一个字符串类型,并且使用小于' < '运算符将其与数字进行比较,奇怪的是,同一查询在IBM DB2 Server Client中可以完美运行。
下面的是我写的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
};
在这里,我们无法将数字900000与字符串类型列进行比较。
我已经尝试过这样
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
};
这给了我一个错误,即LINQ无法识别方法Convert.ToInt64(),并且我知道在T-SQL语句中没有等效的方法来实现转换。
有任何想法吗?
更新:
下面是表格结构
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
***************************************************************************
以下是实体框架生成的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; }
}
此外,我可以在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
两个DB2查询都返回结果
EMP_ID
------
123
234
但是我不能像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
}
谢谢,Gowrish。
假设它将被转换为等效的SQL,请尝试使用类似
where cont.DLR_NO.CompareTo("90000") < 0
至于字符串到整数的转换,您不能保证数据库中的CHARACTER
列将只包含数字,这使得任何坚持将该列转换为整数的程序都是危险的选择。 在这种情况下,先将数字文字转换为字符串(从90000
到"90000"
)比较安全,因此您只需要比较两个字符串即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.