嘿,我正在尽我最大的努力得到以下查询:
SELECT Client, Code, COUNT(SessionID) AS Count
FROM dbo.vw_TrackingAppsCompleteDistinctDate
WHERE (Date > CONVERT(DATETIME, '2012-12-01 00:00:00', 102) AND Date < CONVERT(DATETIME, '2013-01-01 00:00:00', 102))
GROUP BY URL, Code, Client
ORDER BY Client, Code
在ASP.net中转换为LinQ。
到目前为止,我已经:
Public Shared Function SelectByID(ByVal start_date As String, ByVal end_date As String) As vw_TrackingAppsCompleteDistinctDate
Dim db As New MasterDataContext()
Dim query = From r In db.vw_TrackingAppsCompleteDistinctDates Where r.Date > "1/1/2013"
Group r By r.URL, r.Code, r.Client Into g
Select g.All
End Function
有什么建议吗?
我试图创建相应的查询与分组练习。我不能100%确定这个linq会得到你想要的结果,因为我不能测试它。
我猜orderby
可能不同,因为我不知道SQL是在分组之前还是之后排序结果。
var result = from record in dbo.vw_TrackingAppsCompleteDistinctDate
where record.Date.CompareTo( new DateTime( 2012, 12, 1, 0, 0, 0 ) ) > 0
where record.Date.CompareTo( new DateTime( 2013, 1, 1, 0, 0, 0 ) ) < 0
group record by new { record.URL, record.Code, record.Client } into grp
orderby grp.Key.Client, grp.Key.Code
select new
{
Client = grp.Key.Client,
Code = grp.Key.Code,
Count = grp.Select( item => item.SessionID ).Count( )
};
使用SQL到LinQ转换器获得:
Dim query = From
Vw_TrackingAppsCompleteDistinctDates In db.vw_TrackingAppsCompleteDistinctDates
Where
Vw_TrackingAppsCompleteDistinctDates.Date > CDate("2012-12-01 00:00:00")
And Vw_TrackingAppsCompleteDistinctDates.Date < CDate("2013-01-01 00:00:00")
Group Vw_TrackingAppsCompleteDistinctDates By
Vw_TrackingAppsCompleteDistinctDates.URL,
Vw_TrackingAppsCompleteDistinctDates.Code,
Vw_TrackingAppsCompleteDistinctDates.Client
Into g = Group
Order By Client, Code
Select
Client, Code, Count = CType(g.Count(Function(p) p.SessionID <> Nothing), Int64?)