使用Entity Framework运行本机T-SQL函数

时间:2016-10-03 15:28:46

标签: sql sql-server entity-framework tsql

我需要转换类似于this的查询。
但我需要调用本机SQL Server函数(conversionsystem,...)。
如何使用Entity Framework调用它们?
以下是查询示例。

select  
    tbf.nr_list, tbf.cd_lne, tbf.nr_seq, tbf.cd_ref, 
    case 
       when tbri.ds_referencia IS null 
          then tbf.ds_referencia 
          else tbri.ds_referencia 
       end as Ds_Ref, 
       '' as Ds_Med, 
       cast(tbf.qt_tec as MONEY) as Qt_Tec, 
       isnull(tbf.qt_width, 0) as Qt_Width, 
       isnull(tbf.qt_height, 0) as Qt_Height,
       case 
          when tbra.ds_lne IS null        
             then 'null' 
          when right(tbra.ds_lne, 1) = 'X'   
             then 'x'
          when right(tbra.ds_lne, 1) = 'Y' 
             then 'y'
          else tbra.ds_lne 
       end as Ds_Lne, 
       tbro.im_pic as Im_Pic,
       '' as Cd_Compos1, 
       0.00 as Vl_Compos1, 
       rl.dt_end, 
       '' as Ds_Con, 
       0.00 as Al_Mar
   from   
       table_from tbf 
   left join 
       table_ref_udao tbru on (tbru.nr_list = tbf.nr_list and tbru.cd_lne = tbf.cd_lne) 
   left join 
       table_ref_odae tbro  on (tbro.nr_list = tbf.nr_list 
                            and tbro.cd_lne = tbf.cd_lne 
                            and tbro.nr_seq = 1) 
   left join  
       table_ref_adeo tbra on (tbra.nr_list = tbf.nr_list and tbra.cd_lne = tbf.cd_lne) 
   left join 
       table_ref_idio tbri on (tbri.nr_list = tbf.nr_list 
                               and tbri.cd_lne = tbf.cd_lne 
                               and tbri.nr_seq = tbf.nr_seq
                               and tbri.cd_ref = tbf.cd_ref
                               and tbri.cd_idi = tbf.cd_idi)
   left join 
       rom_lista rl on(tbru.nr_list = rl.nr_list)
   where 
       tbf.nr_list = '59846'
   order by 
       tbru.ds_lne, tbf.nr_seq

1 个答案:

答案 0 :(得分:1)

For Case When ...使用

tbri.ds_referencia == null ? tbf.ds_referencia  : tbri.ds_referencia,

对于IsNull ..使用

tbf.qt_width ?? 0,

除了Igor提到的DbFunctions之外,还有SqlFunctions

相关问题