Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 157699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:33:02+00:00 2026-05-11T10:33:02+00:00

How can I group result of a LINQ to SQL query by hours considering

  • 0

How can I group result of a LINQ to SQL query by hours considering that the column type is DateTime?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:33:03+00:00Added an answer on May 11, 2026 at 10:33 am

    Here’s a solution for technical hours (context-less).

    var query = myDC.Orders     .GroupBy(x => x.OrderDate.Hour)     .Select(g => new {         Hour = g.Key,         Amount = g.Sum(x => x.OrderAmount)     }); 

    Which generates this:

    SELECT SUM([t1].[OrderAmount]) AS [Amount], [t1].[value] AS [Hour] FROM (     SELECT DATEPART(Hour, [t0].[OrderDate]) AS [value], [t0].[OrderAmount]     FROM [dbo].[Orders] AS [t0]     ) AS [t1] GROUP BY [t1].[value] 

    Here’s a solution for business hours (context-ful) .

    DateTime zeroDate = new DateTime(2008, 1, 1);  var query = myDC.Orders     .GroupBy(x => System.Data.Linq.SqlClient.SqlMethods.DateDiffHour(zeroDate, x.OrderDate)     )     .Select(g => new { Hours = g.Key, Amount = g.Sum(x => x.OrderAmount) })     .Select(x => new { Hour = zeroDate.AddHours(x.Hours), Amount = x.Amount}); 

    Which generates this:

    SELECT DATEADD(ms, (CONVERT(BigInt,(CONVERT(Float,[t2].[value2])) * 3600000)) % 86400000, DATEADD(day, (CONVERT(BigInt,(CONVERT(Float,[t2].[value2])) * 3600000)) / 86400000, @p1)) AS [Hour], [t2].[value] AS [Amount] FROM (     SELECT SUM([t1].[OrderAmount]) AS [value], [t1].[value] AS [value2]     FROM (         SELECT DATEDIFF(Hour, @p0, [t0].[OrderDate]) AS [value], [t0].[OrderAmount]         FROM [dbo].[Orders] AS [t0]         ) AS [t1]     GROUP BY [t1].[value]     ) AS [t2] 

    Ugh – that bigint/float/milliseconds stuff is ugly and hard to verify. I prefer doing the addition back on the client side:

    var results = myDC.Orders     .GroupBy(x => System.Data.Linq.SqlClient.SqlMethods.DateDiffHour(zeroDate, x.OrderDate)     )     .Select(g => new { Hours = g.Key, Amount = g.Sum(x => x.OrderAmount) })     .ToList()     .Select(x => new { Hour = zeroDate.AddHours(x.Hours), Amount = x.Amount}); 

    Which generates this:

    SELECT SUM([t1].[OrderAmount]) AS [Amount], [t1].[value] AS [Hours] FROM (     SELECT DATEDIFF(Hour, @p0, [t0].[OrderDate]) AS [value], [t0].[OrderAmount]     FROM [dbo].[Orders] AS [t0]     ) AS [t1] GROUP BY [t1].[value] 

    And here’s a third way of doing the contextful hours. This one is very c# friendly, but there’s string logic in the database (yuck).

    var query = myDC.Orders     .GroupBy(x => new DateTime(x.OrderDate.Year, x.OrderDate.Month, x.OrderDate.Day, x.OrderDate.Hour, 0, 0))     .Select(g => new { Hour = g.Key, Amount = g.Sum(x => x.OrderAmount) }); 

    Which generates

    SELECT SUM([t1].[OrderAmount]) AS [Amount], [t1].[value] AS [Hour] FROM (     SELECT CONVERT(DATETIME, (CONVERT(NCHAR(4), DATEPART(Year, [t0].[OrderDate])) + ('-' + (CONVERT(NCHAR(2), DATEPART(Month, [t0].[OrderDate])) + ('-' + CONVERT(NCHAR(2), DATEPART(Day, [t0].[OrderDate])))))) + (' ' + (CONVERT(NCHAR(2), DATEPART(Hour, [t0].[OrderDate])) + (':' + (CONVERT(NCHAR(2), @p0) + (':' + CONVERT(NCHAR(2), @p1)))))), 120) AS [value], [t0].[OrderAmount]     FROM [dbo].[Orders] AS [t0]     ) AS [t1] GROUP BY [t1].[value] 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer if (url.ToLower().Contains("/tools/search.aspx")) { //do stuff here } I would use… May 12, 2026 at 12:59 am
  • Editorial Team
    Editorial Team added an answer DATE_SUB will do part of it depending on what you… May 12, 2026 at 12:59 am
  • Editorial Team
    Editorial Team added an answer You can use Array.Copy() to copy elements from one array… May 12, 2026 at 12:59 am

Related Questions

I'm experimenting with Linq and am having trouble figuring out grouping. I've gone through
Say I have a database table like the following: FileID | FileName | FileSize
I am fairly new to Linq To SQL but trying to run what should
I need to retrieve a set of Widgets from my data access layer, grouped

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.