Linq SELECT AS和CONCAT

时间:2016-02-15 10:33:38

标签: c# sql-server linq select concat

我已经获得了一个SQL查询,我需要将其转换为LINQ,这不是问题,但是在这个特定的查询中有一些我需要帮助的项目,那些是选择列名称为''而且连贯。以下是有问题的查询:

SELECT vessel_idx, 
       vessel_name, 
       spotlist_dp, 
       spotlist_bhp,
       spotlist_deck, 
       spotlist_bp, 
       spotlist_oilrec, 
       spotlist_fifi, 
       spotlist_rov,
       '' AS fixture_charterer,
       '' AS fixture_work,
CONCAT( fixture_charterer ,
        ' ', 
        mid(fixture_start,9,2),
        '/',
        mid(fixture_start,6,2)) AS next_charterer_info, 
        '' AS fixture_location,
        '0000-00-00 00:00:00' AS  fixture_stop, 
        '' AS fixture_note 
FROM tbl_vessels

WHERE vessel_type='AHTS'

AND current_location_spotlist_id = 2
AND fixture_start > '2016-02-12 08:30:00'
AND vessel_status = 'PPT'

ORDER BY fixture_stop

通常的东西,如AND,WHERE等,我可以处理它纯粹是我正在努力的这些部分。选择列为'':

SELECT '' AS fixture_charterer,
       '' AS fixture_work,

和连接

CONCAT( fixture_charterer ,
        ' ', 
        mid(fixture_start,9,2),
        '/',
        mid(fixture_start,6,2)) AS next_charterer_info, 
        '' AS fixture_location,
        '0000-00-00 00:00:00' AS  fixture_stop, 
        '' AS fixture_note

非常感谢

2 个答案:

答案 0 :(得分:0)

SELECT AS表示为:

var demo = from vessel in db.tbl_vessels
         select new {
           fixture_charterer = "", 
           fixture_work = ""
         };

CONCAT是:

var demo = from vessel in db.tbl_vessels
         select new {
           fixture_note = fixture_charterer + " " + next_charterer_info 
         };

答案 1 :(得分:0)

您可以轻松选择""之类的常量:

var q = from v in ....
        ....
        select new 
        {
           ...,
           fixture_charterer = "",
           fixture_work = "",
           next_charterer_info = 
               (v.fixture_charterer ?? "") + " " 
             + (v.fixture_start == null ? "" : v.fixture_start.Substring(8,2)) 
        };