Search code examples
c#anonymous-typesigrouping

IGrouping avoiding anonymous types


I would like to know how to group data avoiding anonymous types, because the keys may be one, two.. according to customer selection.

Example:

We have a class

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

    public int Age { get; set; }

    public string Location { get; set; }

    public char Genre { get; set; }
}

And through a checked list box, you may select parameters to group data, where the options would be:

  • Name
  • Age
  • Location
  • Genre

So we may have between 1 and 4 keys to group data.

I want to avoid a switch case or multiple if statements to get the correct IGrouping data.

I want to avoid:

 public IGrouping<object,Customer> GetGroups(IEnumerable<Customer> data)
 {
    if("Name is selected")
    {
        return data.GroupBy(e => e.Name);
    }
    if("Name and Age are selected")
    {
        return data.GroupBy(e => new { e.Name, e.Age });
    }
}

Solution

  • Not sure if this is something you are looking for, but might be helpful:

    return data.GroupBy(g =>
      new {
        Name = includeName ? g.Name : null,
        Age = includeAge ? g.Age : null,
        ... 
      });