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 615249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:11:59+00:00 2026-05-13T18:11:59+00:00

This is my first real-world LINQ-to-SQL query. I was wondering if I am making

  • 0

This is my first real-world LINQ-to-SQL query. I was wondering if I am making any large, obvious mistakes.

I have a medium-large sized (2M+ records, adding 13k a day) table with data, dataTypeID, machineID, and dateStamp. I’d like to get the average, min, and max of data from all machines and of a specific dataType within a 4 hour period, going back for 28 days.

E.g

DateTime            Avg   Min   Max
1/1/10 12:00AM  74.2  72.1  75.7
1/1/10 04:00AM  74.5  73.1  76.2
1/1/10 08:00AM  73.7  71.5  74.2
1/1/10 12:00PM  73.2  71.2  76.1
etc..
1/28/10 12:00AM  73.1  71.3  75.5

So far I have only been able to group the averages by 1 hour increments, but I could probably deal with that if the alternatives are overly messy.

Code:

var q =
    from d in DataPointTable
    where d.dateStamp > DateTime.Now.AddDays(-28) && (d.dataTypeID == (int)dataType + 1)
    group d by new {
        d.dateStamp.Year,
        d.dateStamp.Month,
        d.dateStamp.Day,
        d.dateStamp.Hour
    } into groupedData
    orderby groupedData.Key.Year, groupedData.Key.Month, groupedData.Key.Day, groupedData.Key.Hour ascending
    select new {
        date = Convert.ToDateTime(
            groupedData.Key.Year.ToString() + "-" +
            groupedData.Key.Month.ToString() + "-" +
            groupedData.Key.Day.ToString() + " " + 
            groupedData.Key.Hour.ToString() + ":00"
            ),
        avg = groupedData.Average(d => d.data),
        max = groupedData.Max(d => d.data),
        min = groupedData.Min(d => d.data)
    };
  • 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. Editorial Team
    Editorial Team
    2026-05-13T18:11:59+00:00Added an answer on May 13, 2026 at 6:11 pm

    If you want 4 hour increments divide the hour by 4 (using integer division) and then multiply by 4 when creating the new datetime element. Note you can simply use the constructor that takes year, month, day, hour, minute, and second instead of constructing a string and converting it.

    var q = 
        from d in DataPointTable 
        where d.dateStamp > DateTime.Now.AddDays(-28) && (d.dataTypeID == (int)dataType + 1) 
        group d by new { 
            d.dateStamp.Year, 
            d.dateStamp.Month, 
            d.dateStamp.Day, 
            Hour = d.dateStamp.Hour / 4
        } into groupedData 
        orderby groupedData.Key.Year, groupedData.Key.Month, groupedData.Key.Day, groupedData.Key.Hour ascending 
        select new { 
            date = new DateTime(
                groupedData.Key.Year, 
                groupedData.Key.Month, 
                groupedData.Key.Day, 
                (groupedData.Key.Hour * 4),
                0, 0), 
            avg = groupedData.Average(d => d.data), 
            max = groupedData.Max(d => d.data), 
            min = groupedData.Min(d => d.data) 
        };
    

    To improve efficiency you might want to consider adding an index on the dateStamp column. Given that you’re only selecting a potentially small range of the dates, using an index should be a significant advantage. I would expect the query plan to do an index seek for the first date, making it even faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently using MySQL version 5.1.6 This is my first real world build and so
This is going to take a bit to explain. I'm creating my first real-world
This is my first real question of need for any of those Gridview experts
I am complete newbie for programming and this is my first real program I
i have two arrays like this first array Array ( [0228] => Array (
Using Dozer to map two objects, I have: /** /* This first class uses
This is my first step into the world of stackoverflow, so apologies if I
I have been reading Real World Haskell , and I am nearing the end,
 Does anyone have a real-world experience in building such a project? I'd like to
I'm working on my first real-world Rails project. So far, I'm mostly mashing up

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.