转换为LINQ查询

时间:2018-04-24 10:57:43

标签: c# linq

任何人都可以帮我将下面的查询作为linq

SELECT E.Cosem_Object, COALESCE(A.Explanation, 'Not Available' ) As 
Explanation ,A.Unit, E.Reading , E.Unit, E.Meter_Date, E.Meter_Time from 
ENERGY_PROFILE E LEFT OUTER JOIN ALL_COSEM_OBJECTS A on 
(SUBSTRING(E.Cosem_Object,0,CHARINDEX('*',E.Cosem_Object + '*',0))) = A.Short_Cosem_Object

更新:

我试试

var query = from o in ENERGY_PROFILE 
            join e in ALL_COSEM_OBJECTS
            on o.Short_Cosem_Object equals e.Short_Cosem_Object
            select new { 
                o.Short_Cosem_Object, o.READING, e.EXPLANATION, e.UNIT 
            };

我需要添加以下行

SUBSTRING(E.Cosem_Object,0,CHARINDEX('*',E.Cosem_Object + '*',0)) in linq query

1 个答案:

答案 0 :(得分:1)

这里是您的SQL查询到LINQ的确切转换,但我不确定它是否正是您所期望的,因为缺少样本和预期结果:

 var set = from E in ENERGY_PROFILE
                     join A in ALL_COSEM_OBJECTS
                     on E.Cosem_Object.Substring(0, (E.Cosem_Object + "*").IndexOf('*')) equals A.Short_Cosem_Object
                     into joinedTb
                     from c in joinedTb.DefaultIfEmpty()
                     select new
                     {   
                         E.Cosem_Object,
                         Explanation = c.Explanation ?? "Not Available",
                         c.Unit,
                         E.Reading,
                         Unit2 = E.Unit,
                         E.Meter_Date,
                         E.Meter_Time
                     };