我有这个问题。
levelnumber = (from b in bv.baLevels
where b.ba_Level_Code == ("0" + SqlFunctions.StringConvert((double)cl.Level_Num.Value)) && b.isActive == 1
select (b.ba_Level_Code + " - " + b.ba_Level_Desc)).FirstOrDefault(),
我的问题是b.ba_Level_Code是字符串。
cl.Level_Num是Int。
baLevels表我有这个ba_Level_code值有008
但是cl是BaCodeLibrary表,这个cl.Level_Num有8个
如果我使用硬编码值进行查询
levelnumber = (from b in bv.baLevels
where b.ba_Level_Code == "008" && b.isActive == 1
select (b.ba_Level_Code + " - " + b.ba_Level_Desc)).FirstOrDefault(),
如何显示008? cl.Level_Num我得到了8.我需要将该值更改为008。
由于
答案 0 :(得分:2)
我无法在LINQ to Enties中看到这样做的好方法。假设您无法修改ba_Level_Code
列的数据类型,我认为您最好的选择是向该表添加持久性computed column,对其进行索引,并在查询中使用该列。
例如,如果您定义了一列
ba_Level_Code_Int as (case when isnumeric(ba_Level_Code)= 1 then cast(ba_Level_Code as int) end) persisted
那么你的LINQ查询可能是
levelnumber = (from b in bv.baLevels
where b.ba_Level_Code_Int == cl.Level_Num.Value && b.isActive == 1
select (b.ba_Level_Code + " - " + b.ba_Level_Desc)).FirstOrDefault();
答案 1 :(得分:1)
您可以使用string.Format
指定将`cl.Level_Num'转换为具有最小位数的字符串:
string.Format("{0:D3}", cl.Level_Num.Value) // would ouput "008" value 8
因此:
levelnumber = (from b in bv.baLevels
where b.ba_Level_Code == string.Format("{0:D3}", cl.Level_Num.Value) && b.isActive == 1
select (b.ba_Level_Code + " - " + b.ba_Level_Desc)).FirstOrDefault()