2011-09-22 43 views
2

我之前在提取数据服务器端的计数方面问过一个问题,并在网站上提供了一个解决方案。建议使用完美的linq,但由于我相对较新,我需要一点深入的帮助。稍微复杂的LINQ for C#

利用约翰的解决方案:

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); 
     } 

    } 
} 

我可以使用上述约翰,杰克和安倍晋三的出现次数由约翰的建议。但如果问题是例如一个稍微复杂

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")}; 

上面的代码的伟大工程,以获取不同的名字出现的次数,但如果我需要的名字出现次数的数量,也数每个人的快乐,悲伤或好的事件的发生。即输出是:姓名,名字的数量,快乐的名字的数量,悲伤的名字的数量,可以名字的数量。如果linq不是这方面的最佳解决方案,我愿意倾听所有的选择。非常感谢您的帮助。

+0

添加至少一个链接到另外一个问题,这样我不明白 – Martin

回答

7

坦率地说,目前还不清楚你是否想要快乐的人总数,或者是因名字而感到高兴的人总数(也是悲伤的,好的)。我会给你一个解决方案,可以给你两个。

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") 
       }; 

然后:

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); 
} 

如果你想总的快乐,悲伤,确定计数你可以说:

Console.WriteLine(nameGroups.Sum(nameGroup => nameGroup.happy)); 

另外,你应该做的enum

public enum Mood { 
    Happy, 
    Sad, 
    Okay 
} 

然后

class Guy { 
    public int Age { get; set; } 
    public string Name { get; set; } 
    public Mood Mood { get; set; } 
} 

,这样就可以代替写:

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) 
      }; 
+0

正是我需要。谢谢您的帮助。 –

1
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); 
      } 

     } 
    } 
+0

谢谢。我实际上已经有了这个解决方案,但是希望将这些结果放到上面的一个列表中。 –

+0

是的,杰森一人就够了。我只是为了清除这些事情 –