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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:25:37+00:00 2026-05-15T06:25:37+00:00

I have the following data structure in my database: LastName FirstName CourseName John Day

  • 0

I have the following data structure in my database:

LastName    FirstName    CourseName
John        Day          Pricing
John        Day          Marketing
John        Day          Finance
Lisa        Smith        Marketing
Lisa        Smith        Finance
etc...

The data shows employess within a business and which courses they have shown a preference to attend. The number of courses per employee will vary (i.e. as above, John has 3 courses and Lisa 2).

I need to take this data from the database and pass it to a webpage view (asp.net mvc).

I would like the data that comes out of my database to match the view as much as possible and want to transform the data using SQl so that it looks like the following:

LastName    FirstName    Course1    Course2    Course3
John        Day          Pricing    Marketing  Finance
Lisa        Smith        Marketing  Finance

Any thoughts on how this may be achieved?


Note: one of the reasons I am trying this approach is that the original data structure does not easily lend itself to be iterated over using the typical mvc syntax:

<% foreach (var item in Model.courseData) { %>

Because of the duplication of names in the orignal data I would end up with lots of conditionals in my View which I would like to avoid.

I have tried transforming the data using c# in my ViewModel but have found it tough going and feel that I could lighten the workload by leveraging SQL before I return the data.

Thanks.

  • 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-15T06:25:38+00:00Added an answer on May 15, 2026 at 6:25 am

    NOTE: I don’t want to use the Database PIVOT feature because the number of classes will most likely change and you have to update your SQL query.

    What you are trying to do should not be done in the View.
    The Model object should be simple enough to be rendered when passed to a View.
    Because, the View shouldn’t have any complicated logic in it.

    Therefore, we need to pre-process the data returned from the SQL Server Database first.

    I am going to assume that you have an object like this because you didn’t mention it has a unique identifier like “CustomerId” or something.

    public class CourseData {
        public string FirstName { get; set; }
        public string LastName  { get; set; }
        public string ClassName { get; set; }
    }
    

    This is going to be my [Model] object.

    public class MyViewModel {
        public IDictionary<string, IList<string>> CourseData { get; set; }
        public int MaxCourseCount { get; set; }
    }
    

    This is my Action method in the Controller.

    public ActionResult MyView()
    {
        IDictionary<string, IList<string>> dict = new Dictionary<string, IList<string>>();
    
        // retrieve data from the database
        IList<CourseData> result = RetrieveData();
        foreach (var item in result)
        {
            // [FirstName] and [LastName] combo will be used as KEY entry
            string key = item.FirstName + " " + item.LastName;
    
            if (dict.ContainsKey(key))
            {
                // add the class name into an existing "string" collection
                dict[key].Add( item.ClassName );
            }
            else
            {
                // instantiate a new "string" collection and add the class name.
                dict[key] = new List<string> { item.ClassName };
            }
        }
    
        // find out which Person has attended the most number of classes.
        int maxCourseCount = 0;
        foreach (var key in dict.Keys)
        {
            int valueCount = dict[key].Count;
            if (valueCount > maxCourseCount)
                maxCourseCount = valueCount;
        }
    
    
        MyViewModel model = new MyViewModel {
            CourseData = dict,
            MaxCourseCount = maxCourseCount
        };
        return View(model);
    }
    

    I aggregated the data into a Data Structure that is easier to render.
    I intentionally added [MaxCourseCount] property to my Model object because it seems like you want to render the Course Data in a <table> format.

    So, all you need to do now is

    1. loop through your Model.CourseData dictionary object’s Keys.
    2. render the Course Names in individual <td> table cells.
    3. render remaining <td> table cells based on your Model’s [MaxCourseCount] value.

    I hope this helps.

    -Soe

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

Sidebar

Related Questions

I have the following data structure (a list of lists) [ ['4', '21', '1',
Let's say we have a solution with the following structure: Project.DAL - Data access
I have the following function that is pulling data from a database. The ajax
I have something like the following data structure: Category StartDateTime EndDateTime =============================================== 1 12/1/2009
I have the following data being returned from my database query: +---------------+-----------+------------------+--------------+-------+ | district_name
I have the following problem in my Data Structures and Problem Solving using Java
Hi I have following data in the table: ID-----startDate----endDate 5549 2008-05-01 4712-12-31 5567 2008-04-17
I have a sql table which have the following data, Id City Country ---
I have the following table and data in SQL Server 2005: create table LogEntries
I have the following sql query for transforming data but is it possible to

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.