I'm using Entity Framework and Dynamic Linq Core to perform some dynamic queries at run time. I have a question on how to write dynamic linq statements to output columns of counts where each column is a field item of another field.
Say I have a table with 3 columns: ID, Gender, and Age (assuming they are only in the 10s).
ID | Gender | Age
01 | male | 20
02 | female | 30
... some thousands of rows
I would like to count the number of people in each gender (groupBy Gender), by their age group.
[{Gender:"male", "20": 120, "30": 200, "40": 300},
{Gender:"female", "20": 300, "30": 200, "40": 1000 }]
I tried to group by age, but this doesn't give exactly what i wanted in the above format, because each gender and age combo becomes a new array item.
var query = db.someDB
.GroupBy("new(Gender, Age)")
.Select("new(Key.Gender as Gender, Key.Age as Age, Count() as value)");
I'm restricted to use dynamic linq core because in my real application, the gender and age fields is up to the user to decide, so their field name will change at run-time.
How would you do it?