I am using a LINQ query in a C# application.
My SQL query looks like this :
select cast(DATEADD(hh, -4, [firstdate]) as DATE) as [Date], COUNT(Name) as 'Total Name',
from Database.dbo.DatabaseName with (nolock)
where DATEDIFF(dd, DATEADD(hh, -4, [firstdate]), getdate()) between 1 and 7
group by cast(DATEADD(hh, -4, [firstdate]) as DATE)
order by cast(DATEADD(hh, -4, [firstdate]) as DATE)
But I don't know how to use it in a LINQ Query
Could anyone help me with this?
You can use the SqlFunctions
class which has DateAdd
and DateDiff
methods, effectively as proxies to the SQL code.
Your LINQ to SQL could probably call DateAdd
just the once though - if you select it, you can then group by the result of that select, and then select the key and count of each group.
See more on this question at Stackoverflow