2013-07-12 26 views
2

我再次完全停留在我正在为提高C#技能而开发的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; 
    } 

我已经知道如何计算平均每年salaryincrease的专业:每年=(totalCurrentSalaries

salaryIncrease - totalStartingSalaries)/ totalYears;

现在我的方法的代码如下所示:

private string GetProfessionWithHighestSalaryIncrease() 
    { 
     List<SalaryInformation> allSalaries = new List<SalaryInformation>(); 
     allSalaries = data.GetSalaryInformation(); 

     //Here I got stuck. I think that this could be solved in a single linq query. 
     //But since I have to make a calculation of EVERY salaryincrease of EVERY 
     //profession, I keep getting stuck on how I should solve this. 

     //After all, I need the sum of every professions CurrentSalaries, the sum 
     //of every professions StartingSalaries and the sum of every professions 
     //yearOfEmployment to be able to calculate that professions salaryincrease 
     //per year. 

     //The profession(string) with the highest salary increase per year, has to be returned. 


    } 

我敢肯定,这可以在一个合并的LINQ/Lambda表达式查询来解决。但我不习惯编写linq和lamdba表达式,所以我在语法上苦苦挣扎。

+2

有一点要注意的 - 如果您使用自动实施的属性,您的所有房产可能会更短... –

+0

您是否正在寻找最高的*平均*薪水增长,或最高的*一年*薪水增长? –

+0

我正在寻找每年最高的平均工资增长率。对不起,如果我不够具体。 – Assassin87

回答

3

可以使用组通过集合进行分区,然后才能由平均工资增加,占据第一位:

string maxSalaryProfession = allSalaries 

    // group by profession 
    .GroupBy(info => info.Profession) 

    // order rows by the average salary increase per profession 
    .OrderByDescending(prof => 
     (prof.Sum(info => info.CurrentSalary) - prof.Sum(info => info.StartSalary)) 
      /prof.Sum(info => info.YearOfEmployment) 
    ) 
    .First().Key; 
+0

这看起来很有趣,可能是答案。我会更多地关注它。谢谢! – Assassin87

-1

一行:

allSalaries.OrderByDescending(s=>(s.CurrentSalary - 
s.StartSalary)/s.YearOfEmployment).First().Profession