slightly complex linq for C#
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 h开发者_运维技巧appy, 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.
var nameGroups = from guy in GuyArray
group guy by guy.name into g
select new {
name = g.Key,
count = g.Count(),
happy = g.Count(x => x.status == "happy"),
sad = g.Count(x => x.status == "sad"),
ok = g.Count(x => x.status == "ok")
};
Then:
foreach(nameGroup in nameGroups) {
Console.WriteLine("Name = {0}, Count = {1}, Happy count = {2}, Sad count = {3}, Okay count = {4}", nameGroup.name, nameGroup.count, nameGroup.happy, nameGroup.sad, nameGroup.ok);
}
If you want the total happy, sad, ok counts you can say:
Console.WriteLine(nameGroups.Sum(nameGroup => nameGroup.happy));
etc.
Additionally, you should make an enum
public enum Mood {
Happy,
Sad,
Okay
}
and then
class Guy {
public int Age { get; set; }
public string Name { get; set; }
public Mood Mood { get; set; }
}
so that you can instead write:
var people = from guy in guyArray
group guy by guy.Name into g
select new {
Name = g.Key,
Count = g.Count(),
HappyCount = g.Count(x => x.Mood == Mood.Happy),
SadCount = g.Count(x => x.Mood == Mood.Sad),
OkayCount = g.Count(x => x.Mood == Mood.Okay)
};
To do so:
class Guy
{
public int age; public string name; string mood;
public Guy( int age, string name,string mood ) {
this.age = age;
this.name = name;
this.mood = mood;
}
}
class Program
{
static void Main( string[] args ) {
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")};
var peepsSad = from f in GuyArray where f.mood=="sad" group f by f.name into g select new { name = g.Key, count = g.Count() };
var peepsHappy = from f in GuyArray where f.mood=="happy" group f by f.name into g select new { name = g.Key, count = g.Count() };
var peepsOk = from f in GuyArray where f.mood=="ok" group f by f.name into g select new { name = g.Key, count = g.Count() };
foreach ( var record in peepsSad ) {
Console.WriteLine( record.name + " : " + record.count );
}
foreach ( var record in peepsHappy ) {
Console.WriteLine( record.name + " : " + record.count );
}
foreach ( var record in peepsOk ) {
Console.WriteLine( record.name + " : " + record.count );
}
}
}
精彩评论