Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic GroupBy

Status
Not open for further replies.

Moregelen

Programmer
Sep 10, 2012
1,222
US
Hello! I was hoping someone could tell me if there is a better, more efficient way to go about this. Basically, here is the situation. I have a program that has a DataTable. Using a settings file, I need to be able to group by multiple columns.

To give an example.. maybe the settings file is xml with something like:

XML:
...
<GroupingColumns>
    <Column>employee_number</Column>
    <Column>clock_OUT_date</Column>
    <Column>job_number</column>
</GroupingColumns>
...

I already know how to read in the XML properly and all of that, and how to do basic LINQ GroupBy, but what I need to do is find a way to dynamically decide which columns that it should group by. Right now the way I'm doing it works... but it really seems ugly and inefficient. Essentially I'm juggling a list of lists of rows. Initially the list of lists just contains a single list of rows, so something like:
Code:
list
{
    list
    {
        row
        {
            employee_number = 1,
            clock_OUT_date = 8-25-2013,
            job_number = 1,
            hours = 10
         },
         row
         {
             employee_number = 1,
             clock_OUT_date = 8-26-2013,
             job_number = 2,
             hours = 10
          }
          row
          {
             employee_number = 2,
             clock_OUT_date = 8-25-2013,
             job_number = 1,
             hours = 8
          }
    }
}

Now, if I were to run, say, GroupBy(job_number) I would end up with a list of two lists of rows, so...

Code:
list
{
    list
    {
        row
        {
            employee_number = 1,
            clock_OUT_date = 8-25-2013,
            job_number = 1,
            hours = 10
         },
          row
          {
             employee_number = 2,
             clock_OUT_date = 8-25-2013,
             job_number = 1,
             hours = 8
          }
    },
    list
    {
         row
         {
             employee_number = 1,
             clock_OUT_date = 8-26-2013,
             job_number = 2,
             hours = 10
          }
    }
}

So every time you do another group by, it goes into each group and generates a new set of lists which get added back to the root list of lists...


Just talking about the way I'm doing it makes me a bit confused trying to keep track of it. This seems ugly to me... anyone know of another way to do this? I can really see this causing me to pull some hair out...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top