How to Construct List> with distinct Key and Concatenate Values if duplicate exist in the Key from a List?
The Class Boss has the sub Class Collection Person for representing List of Employees under him/her.
Create a List> with distinct Key (EmpID) and Values (Roles - It may contain Multiple Values) using Single Inline LINQ Statement.
void Main()
{
List<Boss> BossList = new List<Boss>()
{
new Boss()
{
EmpID = 101,
Name = "Harry",
Department = "Development",
Gender = "Male",
Role = "Manager",
Employees = new List<Person>()
{
new Person() {EmpID = 102, Name = "Peter", Department = "Development",Gender = "Male", Role = "Assistant"},
new Person() {EmpID = 103, Name = "Emma Watson", Department = "Development",Gender = "Female", Role = "Assistant"},
}
},
new Boss()
{
EmpID = 104,
Name = "Raj",
Department = "Development",
Gender = "Male",
Role = "Manager",
Employees = new List<Person>()
{
new Person() {EmpID = 105, Name = "Kaliya", Department = "Development",Gender = "Male", Role = "Assistant"},
new Person() {EmpID = 103, Name = "Emma Watson", Department = "Development",Gender = "Female", Role = "Assistant"},
}
},
new Boss()
{
EmpID = 102,
Name = "Peter",
Department = "Development",
Gender = "Male",
Role = "Manager",
Employees = new List<Person>()
{
new Person() {EmpID = 105, Name = "Kaliya", Department = "Development",Gender = "Male", Role = "Assistant"},
new Person() {EmpID = 103, Name = "Raj", Department = "Development",Gender = "Male", Role = "Assistant"},
}
}
};
List<KeyValuePair<int, string>> EmployeeList = new List<KeyValuePair<int, string>>();
}
public class Person
{
public int EmpID { get; set; }
public string Name { get; set; }
public string Department { get; set; }
public string Gender { get; set; }
public string Role { get; set; }
}
public class Boss
{
public int EmpID { get; set; }
public string Name { get; set; }
public string Department { get; set; }
public string Gender { get; set; }
public string Role { get; set; }
public List<Person> Employees { get; set; }
}
My Expected Output should be
List<KeyValuePair<int, string>> EmployeeList = new List<KeyValuePair<int, string>>()
{
{new KeyValuePair<int, string>(101, "Manager")},
{new KeyValuePair<int, string>(102, "Assistant, Manager")}
{new KeyValuePair<int, string>(103, "Assistant")}
{new KeyValuePair<int, string>(104, "Manager, Assistant")}
{new KeyValuePair<int, string>(105, "Assistant")}
}
Explanation: Here EmpID 101 has a single Role Manager, but EmpID 102 is a Assistant of EmpID 101 and a Manager. So EmpID 102 has dual Role Assistant and Manager. Concatenate the Roles using comma separator. Kindly assist me...
How to achieve the List Members from the List<Boss>BossList
Note: Give your answer using using Single Inline LINQ Statement.
In the Real Scenario, I'm having a Complex Linq Statement, in that this is a small requirement. So, I can't able to split into a multiple statement. So, I need to fix this in an Inline. If multiple Statement Implementation means, I'm having a solution. Kindly assist me...