How can I create a dynamic LINQ query in C # with several possible groups per clauses?


I have been a programmer for some years now but I am a
newcomer to LINQ and C# so forgive me if my question sounds
particularly stupid.

I hope someone may be able to point me in the right
direction. My task is to come up with the ability to form a
dynamic multiple group by linq query within a c# script using
a generic list as a source.

For example, say I have a list containing multiple items with the following

FieldChar1 - character
FieldChar2 - character
FieldChar3 - character
FieldNum1 - numeric
FieldNum2 - numeric

In a nutshell I want to be able to create a LINQ query that
will sum FieldNum1 and FieldNum2 grouped by any one, two or
all three of the FieldChar fields that will be decided at
runtime depending on the users requirements as well as selecting the FieldChar fields in the same query.

I have the dynamic.cs in my project which icludes a GroupByMany extension method but I have to admit I am really not sure how to put these to use. I am able to get the desired results if I use a query with hard-wired group by requests but not dynamically.

Apologies for any erroneous nomenclature, I am new to this language but any advice would be most welcome.

Many thanks


Here is the basic GroupBy example. Let's say we have a simple class like this:

public class Person
    public string Name { get; set; }

    public int Age { get; set; }

    public char Sex { get; set; }

Then, you can use GroupBy like this:

var people = new List<Person> {
    new Person { Name = "Joe", Age = 30, Sex = 'M' },
    new Person { Name = "Liz", Age = 22, Sex = 'F' },
    new Person { Name = "Jim", Age = 22, Sex = 'M' },
    new Person { Name = "Alice", Age = 30, Sex = 'F' },
    new Person { Name = "Jenny", Age = 22, Sex = 'F' }
var groups = people.GroupBy(p => p.Age);

foreach (var group in groups)
    foreach (var person in group)
        Console.WriteLine(person.Name + " - " + person.Age);

As for grouping by multiple properties, see these:
LINQ TO DataSet: Multiple group by on a data table

Basically, it is the same syntax with an anonymous type, for example:

var groups = people.GroupBy(p => new { p.Age, p.Sex });

Note that you may be looking for using multiple OrderBys:

var query = people.OrderBy(p => p.Age).ThenBy(p => p.Sex);
//You can have unlimited ThenBy's

Also note that the result of the last GroupBy statement and the result of this OrderBy statement is NOT the same.