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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:56:35+00:00 2026-05-13T22:56:35+00:00

Consider the following simplified objects and relationships: public class Job{ int JobId; String name;

  • 0

Consider the following simplified objects and relationships:

public class Job{
  int JobId;
  String name;
  String status;
  Discipline disc;
  WorkCategory workCat;
}

public class Discipline {
  int DisciplineId;
  String DisciplineName;
}

public class Workcategory{
   int WorkCategoryId;
   String WorkCategoryName;
}

public Class Grouping{
   Discipline parent;
   List<Workcategory> children;
}

The above models a relationship that a Job is associated with one Discipline and one WorkCategory. The Workcategory is always a child of a specific parent Discipline (one-to-many relationship). We can assume that the relationship of the assigned Discipline and Workcategory will always be valid.

The issue I am facing is when I am trying to create a Grouping result object based on filters that are applied to Jobs. I am not sure If this can be done or if the approach I am taking is even correct. The exact question itself is not clear to me, however the above defines the problem statement.

  1. Can the design be improved?
  2. How can I group Jobs by Discipline and Workcategory?
  3. Do I even need the Grouping class?

I have tried the following (this is my first attempt using Linq), but to no success as my understanding is not complete enough. The other alternative is to first get the Discipline group and loop through the original grouping picking up the related Workcategory.

var grouping = repository.GetJobsWithActiveStatus()
            .GroupBy(x => new {
                                  x.Discipline.DisciplineID, 
                                  x.Discipline.DisciplineName,  
                                  x.Category.WorkCategoryID, 
                                  x.Category.WorkCategoryName
                              })
            .Select(g => new Grouping{
                                 Discipline = new Discipline{
                                                  DisciplineID = g.Key.DisciplineID, 
                                                  Name = g.Key.DisciplineName
                                                  }, 
                                 Categories = ?? // this is where I am lost
                             }});

Edit:
After having posted this, I realized that the inital GroupBy parameters result in one group of item whereas I am looking for Group-SubGroup result.

Edit 2:
To clarify a bit further, I dont want the associated Jobs as part of the result, but rather the Discipline-Workcategory grouping – thus the reason for the Grouping class


Initial solution based on @Obalix

Edit 7-Mar-2010:

This solution does not work – The GroupBy on the object Discipline will yield a unique grouping for each object. I think this is due to it being a reference type. Am I correct?
I initially accepted this as the answer, however after some head scratching realised that my mock data itself was faulty. The initial questions still remain answered.

var result = repository.GetJobsWithActiveStatus()
      .GroupBy(x => x.disc)
      .Select(g => new Grouping
              {
                  Discipline = g.Key,
                  Catergories = g.GroupBy(x=>x.workCat) // <--- The Problem is here, grouping on a reference type
                                 .Select(l=>l.Key) // needed to select the Key's only
                                 .ToList()
              });
  • 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-13T22:56:36+00:00Added an answer on May 13, 2026 at 10:56 pm

    Here is a description how you can implement an hierarchical grouping mechanism.

    A generic way of doing this using LINQ (as shown in the link) is:

    var list = new List<Job>();
    
    var groupedList = list.GroupBy(x => x.disc)
        .Select(g => new {
            Key = g.Key,
            Count = g.Count(),
            WorkCategoryGroups = g.GroupBy(x => x.workCat)
        });
    

    However, the link also describes an alternative way which allows you to do the following:

    var groupedList = list.GroupByMany(x => x.disc, x => x.workCat);
    

    Edit: Following Ahmad’s comment here is the strongly typed version:

    var groupedList = list.GroupBy(x => x.disc)
        .Select(g => new Grouping {
            parent = g.Key,
            children = g.GroupBy(x => x.workCat).ToList()
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

consider the following simplified example: public class Ticket { public int Id; public TicketState
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following Scala case class: case class WideLoad(a: String, b: Int, c: Float,
Consider the (highly simplified) following case: class Dispatcher { public: receive() {/*implementation*/}; // callback
Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>
Consider the following simplified interface inheritence hierarchy: // Starting point: public interface Base {
Consider the following simplified demonstration: Class X contain Class Y . Class Y has
Consider the following: object Main { case class Foo(bar: Int) extends FooList { val
Consider the following (simplified) example: abstract class Bar[T] { val f: PartialFunction[T, T] val

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.