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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:22:25+00:00 2026-05-26T13:22:25+00:00

I have the following code: public IDictionary<string, int> GetCountByDate(DateTime fromDate, DateTime toDate) { var

  • 0

I have the following code:

public IDictionary<string, int> GetCountByDate(DateTime fromDate, DateTime toDate)
{
    var result = Database.Set<User>().Where(x => x.CreatedAt >= fromDate && x.CreatedAt <= toDate).GroupBy(x => new { x.CreatedAt.Year, x.CreatedAt.Month, x.CreatedAt.Day }).Select(x => new { Date = x.Key, Count = x.Count() });

    return result.ToDictionary(x => new DateTime(x.Date.Year, x.Date.Month, x.Date.Day).ToShortDateString(), x => x.Count);
}

This code works perfectly well but the problem is that the DateTime is stored as UTC in the database. The moment I do the GroupBy operation I lose the Time part. So if I tried to convert it back to loca time with the following:

return result.ToDictionary(x => new DateTime(x.Date.Year, x.Date.Month, x.Date.Day).ToLocalTime().ToShortDateString(), x => x.Count);

It would be based on a different time and therefore incorrect. The column has to stay DateTime.

Any suggestions?

  • 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-26T13:22:25+00:00Added an answer on May 26, 2026 at 1:22 pm

    It sounds like the real challenge is that you’ll need to translate twice. First, taking the local DateTime you’re feeding into the method (your question only makes sense if those are local) need to be translated into UTC for the query. But then you need to translate x.CreatedAt into local before you do your grouping—on the server, in other words. This is tricky, but works if you use the SqlFunctions library in System.Data.Objects.SqlClient. Here’s what it’d look like. Messy and I don’t know how performant it’d be, but it should work.

    public IDictionary<string, int> GetCountByDate(DateTime fromDate, DateTime toDate) 
    { 
        DateTime utcFrom = fromDate.ToUniversalTime();
        DateTime utcTo = toDate.ToUniversalTime();
        int offset = (int)(fromDate - utcFrom).TotalHours;
        var result = Database.Set<User>().Where(x => x.CreatedAt >= utcFrom && x.CreatedAt <= utcTo)
            .GroupBy(x => new { 
                SqlFunctions.DateAdd("hh", offset, x.CreatedAt).Value.Year, 
                SqlFunctions.DateAdd("hh", offset, x.CreatedAt).Value.Month, 
                SqlFunctions.DateAdd("hh", offset, x.CreatedAt).Value.Day })
            .Select(x => new { Date = x.Key, Count = x.Count() });
    
        return result.ToDictionary(x => new DateTime(x.Date.Year, x.Date.Month, x.Date.Day).ToShortDateString(), x => x.Count); 
    } 
    

    At that point, the date values in your dictionary object are already in local time, so no need to convert them for presentation.

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

Sidebar

Related Questions

I have the following code: public static T ParameterFetchValue<T>(string parameterKey) { Parameter result =
I have the following custom property in my code: public Dictionary<int, string> ServiceIdList {
I am using EF 4 and have the following code. public static Dictionary<Tuple<int, string>,
I have the following code: public IDictionary<string, string> GetNextBase36(string partitionKey, string rowKey, ref string
I have following code public class TEST { public static void main(String arg[]){ try
i have the following code: public static void Serialize() { List<string> dirs = FileHelper.GetFilesRecursive(fileDirectoryPath);
I have the following code: public class AppDomainArgs : MarshalByRefObject { public string myString;
I have the following code: public static class ScraperMasterUriDetails { public static Dictionary<Guid, string>
I have an entity like so: public class Land { public virtual IDictionary<string, int>
I have a class that contains the following property: public Dictionary<string, int> CommentCounts {

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.