Find the maximum number of custom type lists

I have a false application in which an inheritance chain is like Employee,Manager,Presidentetc.

Employee class looks like

class Employee
    {
        public int EmployeeId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime HireDate { get; set; }

        public Employee() 
        { 
        }
    }

and the Manager class looks like

 class Manager : Employee
    {

        private List<Employee> employeesManaged = new List<Employee>();
        public List<Employee> EmployeesManaged
        {
            get { return employeesManaged; }
            set { employeesManaged = value; }
        }
    }

I would like to write a method that finds a manager who manages the majority of employees (whose EmployeesManaged.Count property is the largest). I currently have two problems.

  • as you will see in the code below, I have to manually add each Manager to the List variable. This is not a long term solution. What would be an effective and concise way to resolve this issue?

  • Linq syntax is invalid.

I think the first problem is most relevant. If there were 1000 Employee objects, how could I get around adding each of them to the list?

 public static Manager  GetBestManager(List<Manager> managerList)
        {
            Manager m = managerList.Select(x => x.EmployeesManaged).Max();

        }

My main method

Employee e = new Employee();
            e.EmployeeId = 101;
            e.FirstName = "Tom";
            e.LastName = "Jones";
            e.HireDate = DateTime.Now.Subtract(TimeSpan.FromDays(40));

            Employee e2 = new Employee();
            e2.EmployeeId = 102;


            Employee e3 = new Employee();
            e3.EmployeeId = 103;


            Manager m = new Manager();
            m.EmployeeId = 201;
            m.EmployeesManaged.Add(e);
            m.EmployeesManaged.Add(e2);

            Manager m2 = new Manager();
            m2.EmployeesManaged.Add(e3);

            List<Manager> mList = new List<Manager>();
            mList.Add(m);
            mList.Add(m2);
            Manager.GetBestManager(mList);

" Employee to Manager

+1
4

1) , . , , . List .

2) LINQ , Manager . :

public static Manager GetBestManager(List<Manager> managerList)
{
    Manager m = managerList.OrderByDescending(x => x.EmployeesManaged.Count).First();
    return m;
}
+8

, , , :

return managerList
       .OrderByDescending(x => x.EmployeesManaged.Count)
       .FirstOrDefault();
+4

. : LINQ ?

"" System.Linq

, . OrderBy , .

+3

, , :

public static Manager GetBestManager(this IList<Manager> managerList) {
    Func<Manager, int> getCount=x => x.EmployeesManaged.Count;
    return managerList.First(x => getCount(x)==managerList.Max(getCount));
}

, . FirstOrDefault , , , , , .

So, why use FirstOrDefaultnull to return and not throw an exception so that the caller can know who has passed the empty list?

+1
source

All Articles