简体   繁体   English

如何计算C#中数组(salary)中某个元素的平均值

[英]How to calculate the average of an element in the array (salary) in C#

what else do I need to add or change to display my employees between the ages of 25 and 35 years with a higher salary than average salary.我还需要添加或更改什么来显示我的 25 至 35 岁员工的工资高于平均工资。 This program only displays employees between the ages of 25 and 35.该程序仅显示 25 到 35 岁之间的员工。

        for (int i = 0; i < employeename; i++)
         {
            while (br.PeekChar() != -1)
            {
                function[i].Name= br.ReadString();
                function[i].Function= br.ReadString();
                function[i].Age= br.ReadInt32();
                function[i].salary= br.ReadInt32();
                function[i].Studies= br.ReadString();

                if ((function[i].Age>25) && (function[i].Age<35))
                {
                    string str = String.Format("|{0,-21}|{1,-9}|{2,-7}|{3,-16}|{4,-12}|", function[i].Name, function[i].Function,
                        function[i].Age.ToString(), function[i].Salary.ToString(), function[i].Studies);
                    Console.WriteLine(str);
                }

            }

        }
        Console.ReadKey();
        br.Close();

I think you need to read all data and calculate avgSalary before filtering:我认为您需要在过滤之前读取所有数据并计算 avgSalary :

Map your data in an Employee class with params Name, Function, Age, Salary, Studies. Map 您在员工 class 中的数据,参数为姓名、Function、年龄、薪水、学业。

    var employees = new List<Employee>();
    while (br.PeekChar() != -1)
    {
        var employee = new Employee() {
           Name= br.ReadString(),
           Function= br.ReadString(),
           Age= br.ReadInt32(),
           Salary= br.ReadInt32(),
           Studies= br.ReadString()
         };
         employees.Add(employee);
     }

var avgSalary = employees.Select(x => x.Salary).Average();

var finalList = employees.Where(x => x.Age > 25 && x.Age < 35 && x.Salary > avgSalary).ToList();

You need to use EF & LINQ for that.为此,您需要使用 EF & LINQ。

best way this: first read all and add to a list then do other operation on list最好的方法是:首先读取所有内容并添加到列表中,然后对列表进行其他操作

here it is a sample!这是一个样本!

sing System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            List<Employee> employees = new List<Employee>();

            for (int i = 0; i < 10; i++)
            {
                employees.Add(new Employee
                {
                    Name ="name "+i,
                    Function = "fun "+i,
                    Age =i+24,
                    Salary =100+i,
                    Studies ="stu"+i
                });
            }



            //here can do any opr on list
            double avg =employees.Average(s => s.Salary);
            var result = employees.Where(x => x.Age > 25 && x.Age < 35 && x.Salary> avg).ToList();

            foreach (var item in result)
            {
                Console.WriteLine("item name:" + item.Name);

            }

            Console.WriteLine("avg :" + avg);
            Console.ReadKey();
        }


        public class Employee
        {
            public string Name { set; get; }
            public string Function { set; get; }
            public int Salary { set; get; }
            public int Age { set; get; }
            public string Studies { set; get; }

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM