获取与最高平均十进制值相关联的字符串

时间:2013-07-01 20:50:05

标签: c#

我正在开发一个Windows窗体应用程序,其目的是计算和显示存储在文本文件中的Salary统计信息。

我现在遇到一项任务有问题:计算当前平均工资最高的职业。

我已将每个薪资统计信息存储为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;
    }

如果您需要更多代码和详细信息,请告诉我,我会将其添加到此主题。

5 个答案:

答案 0 :(得分:2)

尝试使用LINQ

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

这应该可以解决问题。

答案 1 :(得分: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;
    }
}

答案 2 :(得分: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;

答案 3 :(得分: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;

答案 4 :(得分: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;
        }