问题描述
班级老板有子班级收集人员代表他/她的雇员名单.
public class Person { public string Name { get; set; } public string Department { get; set; } public string Gender { get; set; } } public class Boss { public string Name { get; set; } public string Department { get; set; } public string Gender { get; set; } public List<Person> Employees { get; set; } } Boss John = new Boss() { Name = "Harry", Department = "Development", Gender = "Male", Employees = new List<Person>() { new Person() {Name = "Peter", Department = "Development",Gender = "Male"}, new Person() {Name = "Emma Watson", Department = "Development",Gender = "Female"}, } }
我所需的收藏应为
List<Person> Members = new List<Person>() { new Person() {Name = "Harry", Department = "Development",Gender = "Male"}, new Person() {Name = "Peter", Department = "Development",Gender = "Male"}, new Person() {Name = "Emma Watson", Department = "Development",Gender = "Female"}, }
如何使用linq语句从对象John 实现列表成员.请帮助我.
使用linq语句.
推荐答案
您可以简单地这样做:
var list = John.Employees.ToList(); list.Insert(0, new Person { Name = John.Name, Department = John.Department, Gender = John.Gender });
如果您有List<Boss>,那么您可以做类似的事情:
var result = list .SelectMany(x => new [] { new Person { Name = x.Name, Department = x.Department, Gender = x.Gender, EmpID = x.EmpID }} .Concat(x.Employees)) .GroupBy(x => x.EmpID) //Group by employee ID .Select(g => g.First()) //And select a single instance for each unique employee .ToList();
其他推荐答案
您可以使用linq concat
var Members = new [] { new Person { Name = John.Name, Department = John.Department, Gender = John.Gender } } .Concat(John.Employees) .ToList();
其他推荐答案
我建议使用继承:
public class Boss : Person { public List<Person> Employees { get; set; } }
,然后您可以使用以下方式创建所需的列表:
var result = new List<Person> { John }; result.AddRange(John.Employees);
问题描述
The Class Boss has the sub Class Collection Person for representing List of Employees under him/her.
My Requirement is to get the list of persons List<Person> Members from the object John using LINQ Statement.
public class Person { public string Name { get; set; } public string Department { get; set; } public string Gender { get; set; } } public class Boss { public string Name { get; set; } public string Department { get; set; } public string Gender { get; set; } public List<Person> Employees { get; set; } } Boss John = new Boss() { Name = "Harry", Department = "Development", Gender = "Male", Employees = new List<Person>() { new Person() {Name = "Peter", Department = "Development",Gender = "Male"}, new Person() {Name = "Emma Watson", Department = "Development",Gender = "Female"}, } }
My Required Collection should be
List<Person> Members = new List<Person>() { new Person() {Name = "Harry", Department = "Development",Gender = "Male"}, new Person() {Name = "Peter", Department = "Development",Gender = "Male"}, new Person() {Name = "Emma Watson", Department = "Development",Gender = "Female"}, }
How to achieve the List Members from the object John using LINQ Statement. Kindly assist me.
Kindly provide the Solution also for Distinct List<Person> Members from List<Boss> using LINQ Statement.
推荐答案
You can simply do it like this:
var list = John.Employees.ToList(); list.Insert(0, new Person { Name = John.Name, Department = John.Department, Gender = John.Gender });
If you have a List<Boss>, then you can do something like this:
var result = list .SelectMany(x => new [] { new Person { Name = x.Name, Department = x.Department, Gender = x.Gender, EmpID = x.EmpID }} .Concat(x.Employees)) .GroupBy(x => x.EmpID) //Group by employee ID .Select(g => g.First()) //And select a single instance for each unique employee .ToList();
其他推荐答案
You can use LINQ Concat like this:
var Members = new [] { new Person { Name = John.Name, Department = John.Department, Gender = John.Gender } } .Concat(John.Employees) .ToList();
其他推荐答案
I'd recommend using inheritance:
public class Boss : Person { public List<Person> Employees { get; set; } }
And then you can create the list you want by using:
var result = new List<Person> { John }; result.AddRange(John.Employees);