I have an app with an inquiry answers (50 fields, with names like upa1, upa2, upd1, upd6, etc, with possible values "y", "n" and "na").
I can get and count one single field with this:
foreach(var y in items.GroupBy(g => g.upa1)
.Select(group => new {upa1name = group.Key, upa1count = group.Count()})) {
<div>These are the totals for upa1:</div>
<div>@y.upa1name : @y.upa1count</div>
}
And it will output this:
These are the totals for upa1:
y : 30
n : 11
na : 18
I can repeat this for all questions manually, but I need to count the number of "y", "n" and "na" for all questions to form this single output:
These are the totals for all questions:
y : 1342
n : 879
na : 445
I know there are already hundreds of topics about these issues, but the information is far too stretched and over way too many variations on this matter (different tables, joins, etc). Nothin on the "sum count over multiple fields".
Thanks.
Also, on a side note, each line only appears if the answer exists at least once, but I need it to show even if it is a "y : 0". Is this possible?
Lets say you have a class like this:
Now lets populate that list with some test data:
We can create another class that would hold our key along with sumarized values. Something like this:
We could than instantiate a dictionary of key values where key would be your inquiry answer and the value would be an instance of our InquirySummary class.
We would than define an action that would take INquiry object and InquirySummary object and would summarize the values.
Finally you would have all the neccessary things to call an aggregate that would run only once through your list.
This would basically give our dictionary as seed and than we would start looping through the list. First if condition would check if the key already exists in the dicitonary and than would simpy call the sumarize action on that value. Otherwise we would create a new InquirySummary object and would sumarize into him. Each time we are returning the seed which is in our case dictionary.
With only three possible answers you can do this in three separate queries:
This should run reasonably fast.