我有这个SQL查询:
SELECT
Count(Distinct([AgenziaCompleta])) as Age
,Count(Distinct([Corso])) as NumCorsi
,[PV_Prov]
FROM [SvilDB].[dbo].[V_PreiscrittiSistemiCorsiFiniti_ConRegPvCom]
WHERE [SvilDB].[dbo].[V_PreiscrittiSistemiCorsiFiniti_ConRegPvCom].[PV_Reg] = N'XXXXXX'
GROUP BY [PV_Prov]
如何将其变成linq?
由于
答案 0 :(得分:2)
Dim table = V_PreiscrittiSistemiCorsiFiniti_ConRegPvCom
Dim q = table.Where(Function(x) x.PV_Prov = N'XXXXXX')
.GroupBy(Function(x) x.PV_Prov)
.Select(Function(Grp) New With {.Age = Grp.Select(Function(x) x.AgenziaCompleta).Distinct().Count(),
.NumCorsi = Grp.Select(Function(x) x.Corso).Distinct().Count() })
但是因为你过滤了单个PV Prov值,你可以简单地说:
Dim q = table.Where(Function(x) x.PV_Prov = N'XXXXXX')
Dim Age = q.Select(Function(x) x.AgenziaCompleta).Distinct().Count()
Dim NumCorsi = q.Select(Function(x) x.Corso) .Distinct().Count()