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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:40:11+00:00 2026-05-31T13:40:11+00:00

I have a datatable named testable, it has two columns id(primary key, int) and

  • 0

I have a datatable named testable, it has two columns id(primary key, int) and time(datetime). I want to calculate the number of records of every specific month in the table. For example, there are 5 rows in the table,

Id       datetime(d/m/y)
1   12/3/2011
2   15/3/2011
3   4/4/2011
4   1/8/2011
5   19/12/2011

How to write a Linq query to query out the record like this,

Id       datetime           count
1.  1/2011      0
2.  2/2011      0
3.  3/2011      2
4.  4/2011      1
5.  5/2011      0
6.  6/2011      0
7.  7/2011      0
8.  8/2011      1
9.  9/2011      0
10. 10/2011           0
11. 11/2011           0
12. 12/2011           1

I have written a query statement like this,

var query = from c in testtable.AsEnumerable()
                         group c by new
                         {
                             years = Convert.ToDateTime(c.Field<string>("Posted")).Year,
                             months = Convert.ToDateTime(c.Field<string>("Posted")).Month
                         }
                             into d
                             orderby d.Key.years,d.Key.months
                             select new
                             {
                                 Date = String.Format("{0}/{1}",d.Key.months,d.Key.years),                             
                                 Count = d.Count()
                             };

But it only queries out the month 3/4/8/12, it can’t query out other month records.
Anyone can help?

  • 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-31T13:40:12+00:00Added an answer on May 31, 2026 at 1:40 pm

    You need to generate a sequence of all dates and left join your existing query with that sequence.

    var allMonths = Enumerable.Range(1, 12)
        .Select(i => String.Format("{0}/{1}", i, "2011"));
    var query = new[]
    {
        new{ Date= "3/2011" , Count = 2},
        new{ Date= "4/2011" , Count = 1},
        new{ Date= "8/2011" , Count = 1},
        new{ Date= "12/2011", Count = 1},
    };
    
    var query2 = from month in allMonths
                 join date in query on month equals date.Date into g
                 from date in g.DefaultIfEmpty()
                 select new
                 {
                     Date = month,
                     Count = (date == null) ? 0 : date.Count
                 };
    
    foreach (var q in query2)
    {
        Console.WriteLine(q);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one datatable. In that table I have columns named [Total Day], [Present
I have a DataTable with a Name column. I want to generate a collection
I have a Datatable with Id(guid) and Name(string) columns. I traverse through the data
I need to iterate through a DataTable . I have a column there named
I have a datatable with verified 10 rows named dtable. I attempt to bind
I'm binding ListView control to DataTable. The DataTable have a column named ProductID. Is
I need to update/replace the data in datatable.column. The table has a field named
I have a database named dtEmploye with fields employe_id name surname boss_id which has
I have a DataTable (instance named: TimeTable) whose DefaultView (instance named: TimeTableView) I am
I have a database table named call with columns call_time, location, emergency_type and there

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.