当类型与 linq 相同时选择父级和子级

发布于 2024-10-21 10:02:58 字数 542 浏览 1 评论 0原文

我有一个像这样的对象,

public class Employee
{
    public int EmployeeId { get; set; }
    public string Name { get; set; }

    public IList<Employee> Employees{ get; set; }
}

我想做的是选择所有员工,以及单个集合中的父员工。我希望将父级作为分页集合中的第一项。现在我正在做这样的事情

Employee emp = getEmployeeFromService();
var allEmps = new List<Employee>();
allEmps.Add(emp);
allEmps.AddRange(emp.Employees);

var pagedEmployees= (from e in allEmps select e).Skip(offset).Take(pageSize);

有没有更好的方法来使用单个 linq 语句来做到这一点?

I have an object like this

public class Employee
{
    public int EmployeeId { get; set; }
    public string Name { get; set; }

    public IList<Employee> Employees{ get; set; }
}

What I would like to do is select all of the employees, plus the parent employee in a single collection. I want to have the parent as the first item in the collection for paging. Right now I am doing something like this

Employee emp = getEmployeeFromService();
var allEmps = new List<Employee>();
allEmps.Add(emp);
allEmps.AddRange(emp.Employees);

var pagedEmployees= (from e in allEmps select e).Skip(offset).Take(pageSize);

Is there a better way to do this with a single linq statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多情出卖 2024-10-28 10:02:58

像这样:

var pagedEmployees = new [] { emp }.Concat(allEmps).Skip(offset).Take(pageSize);

Like this:

var pagedEmployees = new [] { emp }.Concat(allEmps).Skip(offset).Take(pageSize);
西瑶 2024-10-28 10:02:58

对于员工列表:

 employees.Concat(employees.SelectMany(x => x.Employees)).Skip(offset).Take(pageSize);

示例(输出 7):

public class Employee
{
    public Employee()
    {

    }

    public Employee(int count)
    {
        Employees = new List<Employee>();
        List<Employee> list = Employees as List<Employee>;
        for (int i = 0; i < count; i++)
        {
            list.Add(new Employee());               
        }
    }

    public IEnumerable<Employee> Employees { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        IEnumerable<Employee> employees = new Employee[]
                                            {
                                                new Employee(3),
                                                                new Employee(2)
    };


        IEnumerable<Employee> enumerable = employees.Concat(employees.SelectMany(x => x.Employees));


        Console.WriteLine(enumerable.Count());
        Console.Read();
    }
}

For a list of employees:

 employees.Concat(employees.SelectMany(x => x.Employees)).Skip(offset).Take(pageSize);

Example (outputs 7):

public class Employee
{
    public Employee()
    {

    }

    public Employee(int count)
    {
        Employees = new List<Employee>();
        List<Employee> list = Employees as List<Employee>;
        for (int i = 0; i < count; i++)
        {
            list.Add(new Employee());               
        }
    }

    public IEnumerable<Employee> Employees { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        IEnumerable<Employee> employees = new Employee[]
                                            {
                                                new Employee(3),
                                                                new Employee(2)
    };


        IEnumerable<Employee> enumerable = employees.Concat(employees.SelectMany(x => x.Employees));


        Console.WriteLine(enumerable.Count());
        Console.Read();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文