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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:17:45+00:00 2026-05-25T21:17:45+00:00

I have the following dataset for a TimeTable that needs to be displayed in

  • 0

I have the following dataset for a TimeTable that needs to be displayed in a gridview. Currently a snippet of the dataset looks like this:

SessionNum    TimeStart    TimeStop    Details
----------    ---------    --------    -------
1             08:00        09:00       Math101
1             09:00        10:00       Comp102
1             11:00        12:00       Engn101
2             08:00        09:00       Comp102
2             09:00        10:00       Math101
2             10:00        11:00       Acco103

There are a total of 5 sessions, and I would like for the dataset to look like:

TimeStart    TimeStop    Session1    Session2     ...
---------    --------    --------    --------     ---
08:00        09:00       Math101     Comp102
09:00        10:00       Comp102     Math101
10:00        11:00       -           Acco103
11:00        12:00       Engn101     -

As you will see, there are no aggregate functions required…just grouping, but for the life of me I cannot seem to wrap my head around this one. I have the following LINQ query which generates the first dataset:

List<TimeTable> list = db.TimeTables.OrderBy(o => o.TimeStart).OrderBy(o => o.SessionNum).ToList();

This works fine, and generates the dataset sorted by SessionNum and then TimeStart. My attempt to solve this invlovled the following query:

var result = list.GroupBy(t => t.TimeStart).Select(s => new {
    TimeStart = s.Key,
    Session1 = s.Where(x => x.SessionNum == 1),
    Session2 = s.Where(x => x.SessionNum == 2)
});

This ran, but unfortunately did not work. I know a GroupBy (or a couple) is/are required, but I’m a bit lost from this point forward. I would really appreciate any help towards solving this. Thank you in advance!

  • 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-25T21:17:45+00:00Added an answer on May 25, 2026 at 9:17 pm

    You can’t directly do a pivot query in LINQ. What you can do instead is create a structure like this:

    var record = new
    {
        TimeStart = "10:00",
        TimeStop = "11:00",
        Sessions = new [] { "-", "Acco103", },
    };
    

    When you have a list of these records you must ensure that the Sessions property is array that is the same length as the distinct number of sessions in your entire set of data. Then you can access the session information by indexing into the array.

    This should make more sense after looking at the queries.

    First, query the database for the required data:

    var query =
        from s in db.TimeTables
        orderby s.TimeStop
        orderby s.TimeStart
        group s by new { s.TimeStart, s.TimeStop } into gss
        select new
        {
            gss.Key.TimeStart,
            gss.Key.TimeStop,
            Sessions = gss.ToArray(),
        };
    

    Now determine the distinct set of sessions:

    var sessionNums =
        db.TimeTables
            .Select(s => s.SessionNum)
            .Distinct()
            .OrderBy(n => n)
            .ToArray();
    

    Now process this data in memory (note the .ToArray() call on query):

    var process =
        from q in query.ToArray()
        let lookup = q.Sessions
            .ToLookup(s => s.SessionNum, s => s.Details)
        select new
        {
            q.TimeStart,
            q.TimeStop,
            Sessions = sessionNums
                .Select(n => String.Join(
                    ", ",
                    lookup[n].DefaultIfEmpty("-")))
                .ToArray(),
        };
    

    This is where the hard work is. The lookup creates an easy way to get session detail out for any SessionNum. Calling lookup[n].DefaultIfEmpty("-") ensures that there is at least a single value for each session. The String.Join ensures that if the source data had two sessions for the same session number at the same time that we end up with one value.

    This result is safe no matter how many sessions there are as it will just extend the arrays.

    The output of the process query looks like this:

    process-dump

    Then you can do this query:

    var result =
        from p in process
        select new
        {
            p.TimeStart,
            p.TimeStop,
            Session1 = p.Sessions[0],
            Session2 = p.Sessions[1],
        };
    

    This will effectively “pivot” your results, but you need to explicitly put in each “SessionX” property.

    The output of the result query looks like this:

    result-dump

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

Sidebar

Related Questions

I have a query that looks like this SELECT J.JobID,T.Title FROM JobsTagMap J Left
Suppose that I have the following code: private void UpdateDB(QuoteDataSet dataSet, Strint tableName) {
I currently have the following dataset: { 'component_id':1, '_locales':[ { 'url': 'dutch', 'locale': 'nl_NL'
I currently have the following dataset (simplified): { 'component_id':1, '_locales':[ { 'url': 'dutch', 'locale':
I currently have the following dataset (simplified): { 'component_id':1, '_locales':[ { 'url': 'dutch', 'locale':
Let's say I have the following dataset bodysize=rnorm(20,30,2) bodysize=sort(bodysize) survive=c(0,0,0,0,0,1,0,1,0,0,1,1,0,1,1,1,0,1,1,1) dat=as.data.frame(cbind(bodysize,survive)) I'm aware that
I have following dataset Id Title Group 1 title1 A 2 title2 A 3
I have the following sample dataset (below and/or as CSVs here: http://goo.gl/wK57T ) which
I have the following code: ListBox.DataSource = DataSet.Tables(table_name).Select(some_criteria = match) ListBox.DisplayMember = name The
I have big dataset (but the following is small one for example). I can

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.