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

  • Home
  • SEARCH
  • 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 659869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:05:36+00:00 2026-05-13T23:05:36+00:00

I think I need a way to perform a pivot or crosstab using C#

  • 0

I think I need a way to perform a pivot or crosstab using C# and Linq with an indeterminate number of columns. However, I will add some beginning detail to see where it goes.

< Beginning detail >

Imagine a table that has two fields:

string EventName;
Datetime WhenItHappend;

We want to generate a report like the example below where we group by the EventName, then have a series of columns that calculate some date and time frame count. The difficulty is obviously a variable number of columns.

  Sport   Jan 2010Jan 2009Feb 2010Feb 2009
Basketball    26      18      23      16  
Hockey        28      19      25      18  
Swimming      52      45      48      43  

The customer can create any number of named event counters for a report. The functions are always the same: “Name”, “begin”, “end”.

Any way to assemble this as a single linq IQueryable result in C#?

< End Beginning detail >

As far as SQL goes, I tried creating a date range table {TimeFrameName, begin, end} and joined that to the above table where begin <= whenItHappened and whenItHappened <= end, which produced a nice set of {EventName, TimeFrameName} then did a count(*), but was still left with how to transform the data, making the rows in to columns.

  • 1 1 Answer
  • 1 View
  • 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-13T23:05:36+00:00Added an answer on May 13, 2026 at 11:05 pm

    You have a class / SQL row

    class Thing
    {
        public string EventName;
        public DateTime WhenItHappend;
    }
    

    and you want to group by EventName and WhenItHappened.Month

    // make a collection and add some Things to it
    var list = new List<Thing>();
    list.Add(new Thing(){ .... });
    // etc
    
    var groups = list.GroupBy(t => new { t.WhenItHappend.Month, t.EventName } );
    

    Now you essentially have a 2 dimensional array of collections. You can iterate through these on Month / EventName to get the counts of the items in each collection, which is what you want to display in your table.

    foreach (var row in groups.GroupBy(g => g.Key.EventName))
    {
        foreach (var month in row.GroupBy(g => g.Key.Month))
        {
            var value = month.Count();
        }
    }
    

    The GroupBy operator returns a collection of groups, where each group contains a Key and a subset of the original list that match the specified Key. If you specify a 2 dimensional Key, you can treat the result as a 2 dimensional array of collections.

    GroupBy is a very powerful operator!

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

Sidebar

Related Questions

So, I need some way to implement an undirected network ( I think this
I need to write a rate limiter, that will perform some stuff each time
Friends, I have a strange need and cannot think my way through the problem.
I'm trying to figure out how to perform a sub-select using Linq. I have
I think I need a reverse version of strtok, like: char* p = rstrtok(str,
I think I need something akin to a rails eager loaded query with a
I don't think I need to post my code (there's lots of it) but
Okay, I think I need another pair of eyes to look over this. I'm
This SQL statement example is very close to what I think I need... update
I'm confused regarding each() and .each()... I think I need to use both, but

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.