2013-07-01 26 views
1

我正在研究一個Windows窗體應用程序,其目的是計算並顯示存儲在文本文件中的薪資統計信息。獲取與最高平均十進制值相關聯的字符串

我現在有一個任務存在問題:計算哪個專業誰擁有最高的平均當前工資。

我已將每個薪資統計信息存儲爲SalaryInformation對象。我會告訴你SalaryInformation類是怎麼樣的:

public sealed class SalaryInformation 
{ 
    private string profession; 
    private int yearOfEmployment; 
    private decimal startSalary; 
    private decimal currentSalary; 

    public string Profession 
    { 
     get { return profession; } 
     set { profession = value; } 
    } 

    public int YearOfEmployment 
    { 
     get { return yearOfEmployment; } 
     set { yearOfEmployment = value; } 
    } 

    public decimal StartSalary 
    { 
     get { return startSalary; } 
     set { startSalary = value; } 
    } 


    public decimal CurrentSalary 
    { 
     get { return currentSalary; } 
     set { currentSalary = value; } 
    } 

    public SalaryInformation() 
    { } 

    public SalaryInformation(string p, int yoe, decimal startS, decimal currentS) 
    { 
     profession = p; 
     yearOfEmployment = yoe; 
     startSalary = startS; 
     currentSalary = currentS; 
    } 

我想要做的就是返回一個字符串。 SalaryInformation對象的專業屬性,與最高平均值currentSalary關聯。 請記住,有幾個具有共同職業價值的SalaryInformation對象(例如,三個SalaryInformation對象在屬性專業上具有值「doctor」)。

我開始用這種方法,和我被困在這裏:

public string GetHighestPaidProfession() 
    { 
     string highestPaidProfession = ""; 

     //Gets all the salaryInformation objects and stores them in a list 
     List<SalaryInformation> allSalaries = new List<SalaryInformation>(); 
     allSalaries = data.GetSalaryInformation();   


     //Right here I don't know how to do the rest from here. 
     //I realize that I have to calculate the average currentsalary from every 
     //SalaryInformation I got in the list allSalaries. But then I have to 
     //to get the actual profession which has the highest average currentsalary 
     //among them. It's right there I get stuck. 

     return highestPaidProfession; 
    } 

如果你需要更多的代碼和細節,只是讓我知道,我會添加到這個線程。

+0

我編輯了自己的冠軍。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

+0

[如何使用LINQ選擇具有最小或最大屬性值的對象]的可能重複(http://stackoverflow.com/questions/914109/how-to-use-linq-to-select-object-with-minimum- or-maximum-property-value) - 您正在尋找'AverageBy()',而不是'MinBy()'或'MaxBy()',但邏輯相同。 – Bobson

回答

2

嘗試使用LINQ

return allSalaries.GroupBy(s => s.Profession) 
        .OrderByDescending(g => g.Average(n => n.CurrentSalary)) 
        .FirstOrDefault().Key; 

這應該做的伎倆。

+0

謝謝!它工作得很好:)! – Assassin87

0

試試這個。

void Main() 
{ 
    var allSalaries = new List<SalaryInformation> { 
    new SalaryInformation("doctor", 1, 100, 120), 
    new SalaryInformation("doctor", 1, 120, 150), 
    new SalaryInformation("engineer", 1, 50, 100)}; 

    var profession = allSalaries.GroupBy (s => s.Profession) 
    .Select (s => new {Profession = s.Key, SalaryAvg = s.Average (x => x.CurrentSalary)}) 
    .OrderByDescending (g => g.SalaryAvg) 
    .FirstOrDefault().Profession; 

    Console.WriteLine(profession); 
} 

public class SalaryInformation 
{ 
    private string profession; 
    private int yearOfEmployment; 
    private decimal startSalary; 
    private decimal currentSalary; 

    public string Profession 
    { 
     get { return profession; } 
     set { profession = value; } 
    } 

    public int YearOfEmployment 
    { 
     get { return yearOfEmployment; } 
     set { yearOfEmployment = value; } 
    } 

    public decimal StartSalary 
    { 
     get { return startSalary; } 
     set { startSalary = value; } 
    } 


    public decimal CurrentSalary 
    { 
     get { return currentSalary; } 
     set { currentSalary = value; } 
    } 

    public SalaryInformation() 
    { } 

    public SalaryInformation(string p, int yoe, decimal startS, decimal currentS) 
    { 
     profession = p; 
     yearOfEmployment = yoe; 
     startSalary = startS; 
     currentSalary = currentS; 
    } 
} 
0

嘗試以下(使用Linq

allSalaries = data.GetSalaryInformation(); 
var allSalariesByProfession = allSalaries.GroupBy(x=>x.Profession); 
var averageSalariesByProfession = allSalariesByProfession.Select(group => new {Profession = group.Key, Avg=group.Average(item=>item.CurrentSalary)); 
var highestPayingprofession = averageSalariesByProfession.OrderByDescending(x=>x.Avg).First().Key; 
0
allSalaries = data.GetSalaryInformation(); 

var averageCurrentSalaries = allSalaries.GroupBy(
    si => si.Profession, 
    si => si, 
    (key, g) => new 
    { 
     Profession = key, 
     AverageCurrentSalary = g.Average(si => si.CurrentSalary); 
    }); 

var highestPaidProfession = averageCurrentSalaries.OrderByDescending(
    as => as.AverageCurrentSalary).First().Profession; 
0

這將讓你的薪水最高的行業(平均工資)

 public string GetHighestPaidProfession() 
     { 
      //string highestPaidProfession = ""; no need for the string variable. 

      //Gets all the salaryInformation objects and stores them in a list 
      //just added this elements to list for demonstration only. 
      List<SalaryInformation> allSalaries = new List<SalaryInformation>() 
      { 
       new SalaryInformation("doctor",2010,500.00m,585.00m), 
       new SalaryInformation("doctor",2010,500.00m,585.00m), 
       new SalaryInformation("doctor",2010,500.00m,550.00m), 
       new SalaryInformation("doctor",2010,500.00m,550.00m), 
       new SalaryInformation("manager",2010,400.00m,510.00m), 
       new SalaryInformation("manager",2010,400.00m,490.00m), 
       new SalaryInformation("manager",2010,400.00m,500.00m), 
       new SalaryInformation("manager",2010,400.00m,480.00m), 
       new SalaryInformation("director",2010,600.00m,625.00m), 
       new SalaryInformation("director",2010,600.00m,615.00m) 
      }; 

      Dictionary<string,List<decimal>> results = new Dictionary<string,List<decimal>>(); 

      foreach(SalaryInformation si in allSalaries) 
      { 
       if(results.ContainsKey(si.Profession)) 
       { 
        results[si.Profession].Add(si.CurrentSalary); 
       } 
       else 
       { 
        results.Add(si.Profession,new List<decimal>(){si.CurrentSalary}); 
       } 
      } 

      //this will result in dictionary<string,decimal>,where the dedimal will 
      //already be the average of all salary of each profession. 
      var result = results.ToDictionary(k => k.Key, v => v.Value.Sum()/v.Value.Count); 

      //returns the string in result dictionary which points to the 
      //highest value. 
      return result.Aggregate((l, r) => l.Value > r.Value ? l : r).Key; 
     } 
相關問題