Jack Brown
Jack Brown

Reputation: 580

C# Finding lowest value within an array

    var minSalary = lstEmployeeData.Items.OfType<Employee>().Min(x => x.EmpsSalary);
    var empsWithMinSalary = lstEmployeeData.Items.OfType<Employee>().Where(x => x.EmpsSalary == minSalary);

    string names = "";
    foreach(var e in empsWithMinSalary)
    names += Environment.NewLine + e.EmployeeFirstName;
    string msg = string.Format("The following emplyoees have the lowest salary of {0} : {1}", minSalary, names);
    MessageBox.Show(msg);

Above is my code for the find lowest salary button, however where it says "foreach(var e in empsWithMinSalary) i get an error saying e is already in use?

Upvotes: 0

Views: 992

Answers (3)

Michael Buen
Michael Buen

Reputation: 39393

Let me introduce to you my friend Linq;

using System;
using System.Linq;

class Employee
{
    public string Name { get; set; }
        public decimal Salary { get; set; }
}

public class Test
{
    public static void Main()
    {
        Employee[] emps = new Employee[] 
        {
            new Employee { Name = "John", Salary = 9 },
            new Employee { Name = "Paul", Salary = 8 },
            new Employee { Name = "George", Salary = 6 },
            new Employee { Name = "Ringo", Salary = 6 }
        };
        decimal minSalary = emps.Min(x => x.Salary);

        foreach(var e in emps.Where(e => e.Salary == minSalary))
            Console.WriteLine("{0} {1}", e.Name, e.Salary);
    }
}

Live test: http://ideone.com/BYjiW

Upvotes: 1

digEmAll
digEmAll

Reputation: 57210

Looking at your code it seems to me that you're adding text items to the ListBox, so of course it's not easy to get the salary from that.

Instead, you should pass the Employee object to the the listbox to keep all the necessary informations; in this way, your adding method would be :

    private void btnSave_Click(object sender, EventArgs e)
    {
        var empid = Convert.ToInt32(txtEmployeeID.Text);
        var empfirstname = Convert.ToString(txtEmployeeFirstName.Text);
        var emplastname = Convert.ToString(txtEmployeeLastName.Text);
        var empsalary = Convert.ToDouble(txtSalary.Text);

        var emp = new Employee(empid, empfirstname, emplastname, empsalary);

        lstEmployeeData.Items.Add(emp);
    }

Of course, to get the desired display text you need to re-define ToString() method of Employee, for example like this:

    class Employee
    {
        // other methods...

        public override string ToString()
        {
            return this.EmployeeToString();
        }
    }

Finally, when the button "show employee having the minimum salary" is clicked you should simply do something like this:

private void btnLowestSalary_Click(object sender, EventArgs e)
{
    var minSalary = lstEmployeeData.Items.OfType<Employee>().Min(x => x.Salary);
    var empWithMinSalary = lstEmployeeData.Items.OfType<Employee>()
                                          .First(x => x.Salary == minSalary);

    string msg = string.Format("{0} has the lowest salary of {1}", empWithMinSalary.EmployeeFirstName, minSalary);
    MessageBox.Show(msg);
}

EDIT :

In case more than one employee have the same salary you can do something like this:

private void btnLowestSalary_Click(object sender, EventArgs e)
{
    var minSalary = lstEmployeeData.Items.OfType<Employee>().Min(x => x.Salary);
    var empsWithMinSalary = lstEmployeeData.Items.OfType<Employee>()
                                           .Where(x => x.Salary == minSalary);

    foreach(var e in empsWithMinSalary)
    {
        string msg = string.Format("{0} has the lowest salary of {1}", e.EmployeeFirstName, minSalary);
        MessageBox.Show(msg);
    }
}

Or better:

private void btnLowestSalary_Click(object sender, EventArgs e)
{
    var minSalary = lstEmployeeData.Items.OfType<Employee>().Min(x => x.Salary);
    var empsWithMinSalary = lstEmployeeData.Items.OfType<Employee>()
                                           .Where(x => x.Salary == minSalary);

    string names = "";
    foreach(var e in empsWithMinSalary)
        names += Environment.NewLine + e.EmployeeFirstName;
    string msg = string.Format("The following emplyoees have the lowest salary of {0} : {1}", minSalary, names);
    MessageBox.Show(msg);
}

Upvotes: 3

ninja
ninja

Reputation: 106

This could be done like this:

var lowsalemp = from ee in emp where ee.empsalary == (from e in emp select e.empsalary).Min() select ee;
            foreach (var leastsalemp in lowsalemp)
            {
                Console.WriteLine(leastsalemp.empsalary);
            }

Upvotes: 0

Related Questions