问题
I have written below code to display employee-manager reporting, i have group by so that all employees under one manager should be shown, but it is showing only 1 employee. what i am missing ?
LINQ Group By Logic-
var emp = (from m in employee
group m by m.ManagerId into g
join e1 in employee on g.FirstOrDefault().ManagerId equals e1.EmpId into temp
from t1 in temp.DefaultIfEmpty()
select new
{
EmpId = g.FirstOrDefault().EmpId,
EmployeeName = g.FirstOrDefault().EmployeeName,
Gender = g.FirstOrDefault().Gender,
ManagerId = t1?.ManagerId ?? 0 ,
ManagerName = t1?.EmployeeName ?? "Top of the Chain",
Designation = g.FirstOrDefault().Designation
}).ToList();
I have added implementation of AddEmployee() and RemoveEmployee() as below and calling as below, but Removeemployee does not remove employee from hierarchy ??
employee.Add(new Employee { EmpId = 11, EmployeeName = "Chris Harris", Gender = 'M', ManagerId = 7, Designation = "Executive" });
employee.Add(new Employee { EmpId = 13, EmployeeName = "Rocky Balbo", Gender = 'M', ManagerId = null, Designation = "CEO" });
int? ceo = null;
AddEmployee(new Employee { EmpId = 2, EmployeeName = "Rohan Kumar", Gender = 'M', ManagerId = 13, Designation = "Application Architect" });
AddEmployee(new Employee { EmpId = 1, EmployeeName = "Mohan Kumar", Gender = 'M', ManagerId = 2, Designation = "Tech Lead" });
RemoveEmployee(new Employee { EmpId = 9, EmployeeName = "Rogger Binny", Gender = 'M', ManagerId = 7, Designation = "Sales Executive" });
GetHierarchy(ceo, 0, "");
static void AddEmployee(Employee empDetails)
{
employee.Add(empDetails);
}
static void RemoveEmployee(Employee empDetails)
{
employee.Remove(empDetails);
}
public class Employee
{
public int EmpId { get; set; }
public string EmployeeName { get; set; }
public char Gender { get; set; }
public int? ManagerId { get; set; }
public string Designation { get; set; }
public string ManagerName { get; set; }
}
List<Employee> employee = new List<Employee>();
employee.Add(new Employee { EmpId = 3, EmployeeName = "Andrea Ely", Gender = 'F', ManagerId = 6, Designation = "Tech Head"});
employee.Add(new Employee { EmpId = 4, EmployeeName = "John Eward", Gender = 'M', ManagerId = 3, Designation = "General" });
employee.Add(new Employee { EmpId = 5, EmployeeName = "Lesli Mac", Gender = 'F', ManagerId = 3, Designation = "Staff" });
employee.Add(new Employee { EmpId = 6, EmployeeName = "Satinder Singh", Gender = 'M', ManagerId = 13, Designation = "President" });
employee.Add(new Employee { EmpId = 7, EmployeeName = "Amit Sarna", Gender = 'M', ManagerId = 13, Designation = "Vice President" });
employee.Add(new Employee { EmpId = 8, EmployeeName = "David Steward", Gender = 'M', ManagerId = 7, Designation = "General" });
employee.Add(new Employee { EmpId = 9, EmployeeName = "Rogger Binny", Gender = 'M', ManagerId = 7, Designation = "Sales Executive" });
employee.Add(new Employee { EmpId = 10, EmployeeName = "Emy jackson", Gender = 'F', ManagerId = 7, Designation = "Sales Executive" });
employee.Add(new Employee { EmpId = 11, EmployeeName = "Chris Harris", Gender = 'M', ManagerId = 7, Designation = "Executive" });
employee.Add(new Employee { EmpId = 13, EmployeeName = "Rocky Balbo", Gender = 'M', ManagerId = null, Designation = "CEO" });
var emp = (from m in employee
group m by m.ManagerId into g
join e1 in employee on g.FirstOrDefault().ManagerId equals e1.EmpId into temp
from t1 in temp.DefaultIfEmpty()
select new
{
EmpId = g.FirstOrDefault().EmpId,
EmployeeName = g.FirstOrDefault().EmployeeName,
Gender = g.FirstOrDefault().Gender,
ManagerId = t1?.ManagerId ?? 0 ,
ManagerName = t1?.EmployeeName ?? "Top of the Chain",
Designation = g.FirstOrDefault().Designation
}).ToList();
Console.WriteLine("--------------Printing Manager-Employee Hierarchy-------------");
foreach (var empDetails in emp)
{
Console.WriteLine("Emp Name is {0} ManagerName is {1}", empDetails.EmployeeName, empDetails.ManagerName);
}
回答1:
Use a recursive algorithm :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static List<Employee> employees;
static void Main(string[] args)
{
employees = new List<Employee>();
employees.Add(new Employee { EmpId = 3, EmployeeName = "Andrea Ely", Gender = 'F', ManagerId = 6, Designation = "Tech Head"});
employees.Add(new Employee { EmpId = 4, EmployeeName = "John Eward", Gender = 'M', ManagerId = 3, Designation = "General" });
employees.Add(new Employee { EmpId = 5, EmployeeName = "Lesli Mac", Gender = 'F', ManagerId = 3, Designation = "Staff" });
employees.Add(new Employee { EmpId = 6, EmployeeName = "Satinder Singh", Gender = 'M', ManagerId = 13, Designation = "President" });
employees.Add(new Employee { EmpId = 7, EmployeeName = "Amit Sarna", Gender = 'M', ManagerId = 13, Designation = "Vice President" });
employees.Add(new Employee { EmpId = 8, EmployeeName = "David Steward", Gender = 'M', ManagerId = 7, Designation = "General" });
employees.Add(new Employee { EmpId = 9, EmployeeName = "Rogger Binny", Gender = 'M', ManagerId = 7, Designation = "Sales Executive" });
employees.Add(new Employee { EmpId = 10, EmployeeName = "Emy jackson", Gender = 'F', ManagerId = 7, Designation = "Sales Executive" });
employees.Add(new Employee { EmpId = 11, EmployeeName = "Chris Harris", Gender = 'M', ManagerId = 7, Designation = "Executive" });
employees.Add(new Employee { EmpId = 13, EmployeeName = "Rocky Balbo", Gender = 'M', ManagerId = null, Designation = "CEO" });
int? ceo = null;
GetHierarchy(ceo, 0, "");
}
static void GetHierarchy(int? managerID, int level, string managerName)
{
List<Employee> group = employees.Where(x => x.ManagerId == managerID).ToList();
foreach (Employee empDetails in group)
{
Console.WriteLine("{0}Emp Name is {1} ManagerName is {2}", new string(' ', 5 * level), empDetails.EmployeeName, managerName);
GetHierarchy(empDetails.EmpId, level + 1, empDetails.EmployeeName);
}
}
}
public class Employee
{
public int EmpId { get; set; }
public string EmployeeName { get; set; }
public char Gender { get; set; }
public int? ManagerId { get; set; }
public string Designation { get; set; }
public string ManagerName { get; set; }
}
}
回答2:
Update as per comment
When calling RemoveEmployee
your are create a new object
, that's mean a new reference in memory. then the two objects
are not equal(The object in employee
list and the new object
), but the values(values of empId
...) are the same.
To delete element from employee
, you have two solutions:
1 - use RemoveAll
, and filtering by empId
or create an other method that take empId
, like the following code:
void RemoveEmployee(Employee empDetails)
{
employee.RemoveAll(emp => emp.EmpId == empDetails.EmpId);
}
void RemoveEmployee(int empId)
{
employee.RemoveAll(emp => emp.EmpId == empId);
}
2 - Filter when calling your RemoveEmployee
, like the following code:
RemoveEmployee(employee.First(emp => emp.EmpId == 9));
Old solution
If in understand correctly, the problem here is with ManagerName
, your code is good, but you need to avoid FirstOrdDefault
to get the expected result,by looping for each element in g
, like the following code:
var groupedEmp = (from m in employee
group m by m.ManagerId into g
join e1 in employee on g.Key equals e1.EmpId into temp
from t1 in temp.DefaultIfEmpty()
select new
{
g = g,
t1 = t1
});
var empList = groupedEmp
.SelectMany(x => x.g.Select(y =>
new Employee
{
EmpId = y.EmpId,
EmployeeName = y.EmployeeName,
Designation = y.Designation,
Gender = y.Gender,
ManagerId = y.ManagerId ?? 0,
ManagerName = x.t1?.EmployeeName ?? "Top"
})).ToList();
Test
foreach(Employee employe in empList)
{
Console.WriteLine($"EmpId : {employe.EmpId}, EmpName : {employe.EmployeeName}, MngId : {employe.ManagerId}, MngName : {employe.ManagerName}");
}
Result
EmpId : 3, EmpName : Andrea Ely, MngId : 6, MngName : Satinder Singh
EmpId : 4, EmpName : John Eward, MngId : 3, MngName : Andrea Ely
EmpId : 5, EmpName : Lesli Mac, MngId : 3, MngName : Andrea Ely
EmpId : 6, EmpName : Satinder Singh, MngId : 13, MngName : Rocky Balbo
EmpId : 7, EmpName : Amit Sarna, MngId : 13, MngName : Rocky Balbo
EmpId : 8, EmpName : David Steward, MngId : 7, MngName : Amit Sarna
EmpId : 9, EmpName : Rogger Binny, MngId : 7, MngName : Amit Sarna
EmpId : 10, EmpName : Emy jackson, MngId : 7, MngName : Amit Sarna
EmpId : 11, EmpName : Chris Harris, MngId : 7, MngName : Amit Sarna
EmpId : 13, EmpName : Rocky Balbo, MngId : 0, MngName : Top
i hope this help.
来源:https://stackoverflow.com/questions/60872259/organization-chart-group-by-manager-to-show-all-employees-under-him