在c#linq中同时选择父母和子女收集到某些模型的列表[英] Select Parent and Child Collection to a List of some model simultaniously in C# Linq

本文是小编为大家收集整理的关于在c#linq中同时选择父母和子女收集到某些模型的列表的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

班级老板有子班级收集人员代表他/她的雇员名单.

我的要求是获取人列表List<Person> Members 来自对象John 使用Linq语句.

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);

本文地址:https://www.itbaoku.cn/post/1557113.html

问题描述

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);
相关标签/搜索