如果列表中的键中存在重复项,则使用不同的键和连接值构造 KeyValuePair<int, string>
Construct KeyValuePair<int, string> with distinct Key and Concat Values if duplicate exist in the Key from a List
如果列表中的键中存在重复项,如何使用不同的键构造列表>并连接值?
Class 老板在 him/her 下有子 Class 代表员工列表的集合人。
创建一个列表> 具有不同的键 (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 具有双重角色 Assistant 和 Manager。使用逗号分隔符连接角色。请帮助我...
如何实现List<Boss>BossList
的List Members
Note: Give your answer using using Single Inline LINQ Statement.
在真实场景中,我有一个复杂的 Linq 语句,因为这是一个小要求。所以,我无法拆分成多个语句。所以,我需要在内联中解决这个问题。如果多语句实现意味着,我有一个解决方案。请帮助我...
这个这个:
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 ();
}
注意 将 return 只有一个 ID 为 104 的项目,因为没有 ID 为 104 的助手。
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)
如果列表中的键中存在重复项,如何使用不同的键构造列表>并连接值?
Class 老板在 him/her 下有子 Class 代表员工列表的集合人。
创建一个列表> 具有不同的键 (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 具有双重角色 Assistant 和 Manager。使用逗号分隔符连接角色。请帮助我...
如何实现List<Boss>BossList
Note: Give your answer using using Single Inline LINQ Statement.
在真实场景中,我有一个复杂的 Linq 语句,因为这是一个小要求。所以,我无法拆分成多个语句。所以,我需要在内联中解决这个问题。如果多语句实现意味着,我有一个解决方案。请帮助我...
这个这个:
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 ();
}
注意 将 return 只有一个 ID 为 104 的项目,因为没有 ID 为 104 的助手。
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)