-1

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...

B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
  • 6
    "Give your answer using using Single Inline LINQ Statement". Why the artificial restriction? Is this homework? – JLRishe Mar 18 '16 at 07:38
  • @JLRishe Its not a home work. 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 a Inline. If multiple Statement Implementation means, I'm having a solution. Kindly assist me... – B.Balamanigandan Mar 18 '16 at 07:40
  • i think the linq would not be suitable here. it becomes harder to understand and you may forget it after some time. this is what i have experienced... – M.kazem Akhgary Mar 18 '16 at 08:32

2 Answers2

2

This this:

var result = BossList.SelectMany (boss => boss.Employees).
    Select (employe => new { EmpID = employe.EmpID, Role = employe.Role }).
    Concat (BossList.Select (boss => new { EmpID = boss.EmpID, Role = boss.Role })).
    GroupBy (person => person.EmpID,
        (key, values) => new KeyValuePair<int, string> (key,
            string.Join (", ", values.Select (person => person.Role).Distinct ()))).
    OrderBy (person => person.Key). // If key order matters.
    ToList ();
}

NOTE will return only one item with ID 104, because there is not Assitant with id 104.

Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34
1
BossList
    .SelectMany(boss => boss.Employees)
    .Select(emp => new {emp.EmpID, emp.Role})
    .Union(BossList.Select(boss => new {boss.EmpID, boss.Role}))
    .GroupBy(kv=>kv.EmpID)
    .Select(kv=>new {ID= kv.Key, Roles = String.Join(",", kv.Select(kkv=>kkv.Role))})
    .ToDictionary(kv => kv.ID, kv=>kv.Roles)
Helic
  • 907
  • 1
  • 10
  • 25