如何构建列表>具有不同的Key和Concatenate值,如果List中的Key存在重复吗?
Class Boss拥有子类收集人,代表他/她下的雇员名单。
创建列表>具有不同的密钥(EmpID)和值(角色 - 它可能包含多个值)使用单内联LINQ语句。
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; }
}
我的预期输出应为
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")}
}
说明: 这里EmpID 101有一个角色管理器,但EmpID 102是EmpID 101的助手和经理。所以EmpID 102有双重角色助理和经理。使用逗号分隔符连接角色。请帮助我...
如何从List<Boss>BossList
注意:使用单行内联LINQ声明给出答案。
在Real Scenario中,我有一个复杂的Linq声明,因为这是一个小要求。所以,我无法分成多个声明。所以,我需要在内联中解决这个问题。如果多个Statement Implementation意味着,我有一个解决方案。请帮助我...
答案 0 :(得分:2)
这个:
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 ();
}
注意只会返回一个ID为104的项目,因为没有ID为104的Assitant。
答案 1 :(得分: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)