2011-09-22 59 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

是的,傑森一人就夠了。我只是爲了清除這些事情 –