2011-09-22 9 views
2

나는 데이터 서버 측의 카운트를 검색하기 전에 질문을했고 사이트에서 솔루션을 제공 받았다. 제안은 linq를 사용하는 것이 었습니다. 그러나 비교적 익숙하지 않았기 때문에 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

(210)

하고 대신 쓸 수 있도록 다음

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

네, jason one으로 충분합니다. 내 것은 단지 물건을 치우는 것이었다. –