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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:19:19+00:00 2026-05-24T13:19:19+00:00

I have a table with an event id pk, date column and an event

  • 0

I have a table with an event id pk, date column and an event type column.
I want to use linq to get the count of events in each day.
The issue is that the table is sparse, i.e. values are not stored in days which did not have any events.

Since I want to use this data for a line chart, I need to fill out the data with the missing dates and give them a value of zero.

Is these any way to do this inside linq? or do I have to do this manually?

Is there any recommended method of doing this?

Edit:

I created the following method:

public string GetDailyData(int month, int year)
    {
        int days = DateTime.DaysInMonth(year,month);
        DateTime firstOfTheMonth = new DateTime(year, month, 1);
        PaymentModelDataContext db = new PaymentModelDataContext();
        var q = from daynumber in Enumerable.Range(0, days)
                let day = firstOfTheMonth.AddDays(daynumber)
                join data in db.TrackingEvents on day equals data.timestamp.Day into d2
                from x in d2.DefaultIfEmpty()
                select Tuple.Create(x.Key, x.Value);

        return ParseJson(q);
    }

The problem is I get an error on the ‘join’ keyword:
“The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to ‘GroupJoin'”

Edit 2:
I made the changes suggested and tried to group the results.
When I send them to the parsing function, I get a null object ref error.
Here is the new code:

[WebMethod]
    public string GetDailyData(int month, int year)
    {
        int days = DateTime.DaysInMonth(year, month);
        DateTime firstOfTheMonth = new DateTime(year, month, 1);
        PaymentModelDataContext db = new PaymentModelDataContext();
        var q = from daynumber in Enumerable.Range(0, days)
                let day = firstOfTheMonth.AddDays(daynumber)
                join data in db.TrackingEvents on day equals data.timestamp.Date into d2
                from x in d2.DefaultIfEmpty()
                group x by x.timestamp.Date;

        return ParseJson(q);
    }

And the parsing function:

private string ParseJson<TKey, TValue>(IEnumerable<IGrouping<TKey, TValue>> q)
    {
        string returnJSON = "[{ \"type\" : \"pie\", \"name\" : \"Campaigns\", \"data\" : [ ";
        foreach (var grp in q)
        {
            double currCount = grp.Count();
            if (grp.Key != null)
                returnJSON += "['" + grp.Key + "', " + currCount + "],";
            else
                returnJSON += "['none', " + currCount + "],";
        }
        returnJSON = returnJSON.Substring(0, returnJSON.Length - 1);
        returnJSON += "]}]";

        return returnJSON;
    }
  • 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-24T13:19:20+00:00Added an answer on May 24, 2026 at 1:19 pm

    You should be able to use LINQ. One method is to use Enumerable.Range to create a collection of dates between the min and max dates, and then perform an outer join (using GroupJoin) against the sparse table. (See MSDN Reference: How to Perform Outer Joins (C# Programming Guide))

    For instance, if numdays is the date range (in days), MinDate is the initial date, and SparseData is your sparse data, and SparseData has an instance property Day that specifies the date, then you might do:

    var q = Enumerable.Range(0, numdays)
            .Select(a => MinDate.AddDays(a))
            .GroupJoin(
               SparseData,
               q=>q,
               sd=>sd.Day,
               (key, value) =>
                   Tuple.Create(
                         key,
                         value.DefaultIfEmpty().First()
                   )
              );
    

    Or, equivalently,

      var q2 = from daynumber in Enumerable.Range(0, numdays)
               let day = MinDate.AddDays(daynumber)
               join data in SparseData on day equals data.Day into d2
               from x in d2.DefaultIfEmpty()
               select Tuple.Create(x.Key, x.Value);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a table that has event name and event date. if i want
I have a table called Event. A column has duration--which is...how long that event
I have a table which looks something like Event ID Date Instructor 1 1/1/2000
I have a table in MySQL with multiple of reported events (primary key, date
I have two tables, one called Events and one called Documents. Each table has
I have a table for orders and it has 2 column with DATE types:
I have a table with columns: Date, Phone, Name, and Event. I need a
I have an Oracle table which contains event log messages for an application. We
I have a table that functions as an event log and stores the users
I have a table with a column event_time . How can I select two

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.