Convert SQL to Linq To SQL
I have the following SQL query that I'd like to convert in to LinqToSql
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)
The Date column calculation above is a hack that is often used for stripping the time component from a datetime value, essentially setting the time component to zero.
The resultant resultset looks like this:
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
My L2S equivalent is as follows:
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}
The resultset look like this:
AppId Model Date
0219d640-6e56-4c09-be8a-a4dc4777bf31 M1 94108
04d2de8b-646f-4433-8738-76103344e6b2 M2 94206
07d09852e9ca439cb0c573c8292e64f5 M2 94103
The query works at this point, but I don't have the "COUNT(Timestamp)" column here yet and am trying to figure out how to represent this in LinqToSql. Also, the date is represented as the date boundaries from the DateDiff function and not real datetime values. Can someone have a look at my L2S translation and see if I am missing anything? Also, how can I get the count of timestamps added to this translation?
============UPDATE==================
From John's guidance below, the final Linq query looks like this:-
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()
}
It looks like you need to refer to "gApp.Key.Timestamp" instead of "app.Timestamp", since you're selecting from "gApp", and "app" is no longer in context.
Edit
So, your current problem is getting the count. That should be as simple as:
select new {App = gApp.Key.AppId, Model = gApp.Key.Model, Date = gApp.Key.Date, Count = gApp.Count() }
精彩评论