我有以下要转换为LinqToSql
的SQL查询SELECT recWishList.AppId, phyDM.Name,
DATEADD(dd, DATEDIFF(dd, 0, recWishList.Timestamp), 0) AS Date,
COUNT(recWishList.Timestamp) AS Downloads
FROM
dbo.RecommendWishlist recWishList
INNER JOIN VirtualDevice virDevice
on recWishList.VirtualDeviceId = virDevice.Id
INNER JOIN PhysicalDeviceModel phyDM
on virDevice.PhysicalDeviceModelId = phyDM.Id
GROUP BY
AppId, phyDM.Name, DATEADD(dd, DATEDIFF(dd, 0, recWishList.Timestamp), 0)
上面的Date列计算是一个hack,通常用于从datetime值中剥离时间组件,实质上是将时间组件设置为零。
结果结果集如下所示:
AppId Model Date Downloads
0219d640-6e56-4c09-be8a-a4dc4777bf31 M1 2010-08-30 00:00:00.000 1
04d2de8b-646f-4433-8738-76103344e6b2 M2 2010-12-06 00:00:00.000 1
07d09852e9ca439cb0c573c8292e64f5 M2 2010-08-25 00:00:00.000 1
07d09852e9ca439cb0c573c8292e64f5 M2 2010-09-13 00:00:00.000 1
07d09852e9ca439cb0c573c8292e64f5 M1 2010-08-25 00:00:00.000 2
我的L2S等价物如下:
from app in RecommendWishlists
join virtualDevice in VirtualDevices on app.VirtualDeviceId equals virtualDevice.Id
join physicalDeviceModel in PhysicalDeviceModels on virtualDevice.PhysicalDeviceModelId equals physicalDeviceModel.Id
group app by new {app.AppId, Model=physicalDeviceModel.Name, Date = SqlMethods.DateDiffDay(DateTime.Parse("1/1/1753"),app.Timestamp)} into gApp
select new {App = gApp.Key.AppId, Model = gApp.Key.Model, Date = gApp.Key.Date}
结果集如下所示:
AppId Model Date
0219d640-6e56-4c09-be8a-a4dc4777bf31 M1 94108
04d2de8b-646f-4433-8738-76103344e6b2 M2 94206
07d09852e9ca439cb0c573c8292e64f5 M2 94103
此时查询正常工作,但我还没有“COUNT(时间戳)”列,我正试图弄清楚如何在LinqToSql中表示这一点。此外,日期表示为DateDiff函数的日期边界,而不是实际日期时间值。有人可以查看我的L2S翻译,看看我是否遗漏了什么?另外,如何获取添加到此翻译的时间戳计数?
============ UPDATE ==================
根据John的指导,最终的Linq查询如下所示: -
from recwishlist in RecommendWishlists
join virdevice in VirtualDevices on recwishlist.VirtualDeviceId equals virdevice.Id
join phydm in PhysicalDeviceModels on virdevice.PhysicalDeviceModelId equals phydm.Id
group new {recwishlist, phydm} by new {
recwishlist.AppId,
phydm.Name,
Date =SqlDateTime.MinValue.Value.AddDays(
SqlMethods.DateDiffDay(SqlDateTime.MinValue.Value,recwishlist.Timestamp))
} into gApp
select new {
gApp.Key.AppId,
gApp.Key.Name,
gApp.Key.Date,
Downloads = (Int64?)gApp.Count()
}
答案 0 :(得分:0)
看起来您需要引用“gApp.Key.Timestamp”而不是“app.Timestamp”,因为您从“gApp”中选择,而“app”不再处于上下文中。
修改强>
所以,你当前的问题正在计算中。这应该很简单:
select new {App = gApp.Key.AppId, Model = gApp.Key.Model, Date = gApp.Key.Date, Count = gApp.Count() }