I asked a question before on retrieving the count of data server side and was provided a solution on the site. The suggestion was to use linq which worked perfectly however since i’m relatively new to it I need a little in depth help.
Using John’s solution:
class Guy
{
public int age; public string name;
public Guy( int age, string name ) {
this.age = age;
this.name = name;
}
}
class Program
{
static void Main( string[] args ) {
var GuyArray = new Guy[] {
new Guy(22,"John"),new Guy(25,"John"),new Guy(27,"John"),new Guy(29,"John"),new Guy(12,"Jack"),new Guy(32,"Jack"),new Guy(52,"Jack"),new Guy(100,"Abe")};
var peeps = from f in GuyArray group f by f.name into g select new { name = g.Key, count = g.Count() };
foreach ( var record in peeps ) {
Console.WriteLine( record.name + " : " + record.count );
}
}
}
I can get the count of occurrences of John, Jake and Abe using the above as suggested by John. However what if the problem was a little more complicated for instance
var GuyArray = new Guy[] {
new Guy(22,"John", "happy"),new Guy(25,"John", "sad"),new Guy(27,"John", "ok"),
new Guy(29,"John", "happy"),new Guy(12,"Jack", "happy"),new Guy(32,"Jack", "happy"),
new Guy(52,"Jack", "happy"),new Guy(100,"Abe", "ok")};
The above code works great to retrieve the number of occurrences of the different names but what if I need the number of occurences of the names and also the number of occurrences of each person who is happy, sad or ok as well. ie output is : Name, count of names, count of names that are happy, count of names that are sad, count of names that are ok. If linq isn’t the best solution for this, I am prepared to listen to all alternatives. Your help is much appreciated.
Frankly, it’s not clear if you want the total number of people that are happy, or the total number of people that are happy by name (also for sad, ok). I’ll give you a solution that can give you both.
Then:
If you want the total happy, sad, ok counts you can say:
etc.
Additionally, you should make an enum
and then
so that you can instead write: