Reputation: 1
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. This program only displays employees between the ages of 25 and 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();
Upvotes: 0
Views: 560
Reputation: 247
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; }
}
}
}
Upvotes: 0
Reputation: 2598
I think you need to read all data and calculate avgSalary before filtering:
Map your data in an Employee class with params Name, Function, Age, Salary, Studies.
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.
Upvotes: 1