I'm trying to run a query where the 'GroupBy' clause will group the results differently according to say, an enum value which would be databound to a combobox etc.
I tried to do this with a switch case, but this resulted in compiler error CS0411 on the 'GroupBy' clause, because as I understand it, the type of the object returned from the lambda will be different depending on which case is executed.
How can I neatly achieve this functionality?
I have enclosed a minimal example of the problem below, as a console program.
class Program
{
static List<Person> People;
enum GroupBy
{
Month,
Year
}
static void Main(string[] args)
{
People = new List<Person>();
People.Add(new Person()
{
Dob = new DateTime(2000,10,1),
Name = "James",
IsSelected = false
});
People.Add(new Person()
{
Dob = new DateTime(2000, 5, 1),
Name = "Anne",
IsSelected = true
});
People.Add(new Person()
{
Dob = new DateTime(2000, 5, 23),
Name = "Bob",
IsSelected = true
});
People.Add(new Person()
{
Dob = new DateTime(1999, 1, 15),
Name = "Kate",
IsSelected = false
});
// Obviously, in the real thing, the grouping value would be
// databound to a combobox or something.
GroupBy grouping = GroupBy.Month;
var groups = People
.OrderBy(x => x.Dob)
.GroupBy(x => /* PROBLEM LINE - Gives compiler error CS0411 */
{
switch(grouping)
{
case GroupBy.Month:
return new { x.Dob.Year, x.Dob.Month };
case GroupBy.Year:
return new { x.Dob.Year};
default:
throw new Exception("No can do");
}
});
Console.WriteLine("Grouping is: " + grouping + "\r\n");
foreach(var group in groups)
{
Console.WriteLine("Group: " + group.Key);
foreach(Person p in group)
{
Console.WriteLine("\t" + p.Name + ", " + p.Dob);
}
Console.WriteLine("");
}
Console.ReadKey();
}
}
public class Person
{
public DateTime Dob { get; set; }
public string Name { get; set; }
public bool IsSelected { get; set; }
}