修改for循环中的IEnuemerable

时间:2017-04-04 00:20:01

标签: c# linq for-loop recursion dto

如何修改IEnuemerable<ProductLineDto>

内的嵌套元素

所以基本上循环看起来像product lines&gt; project types&gt; projects&gt; sub projects&gt;它包含更多sub projectsactivities

问题是我无法修改应该在递归函数activities

中发生的subproject的{​​{1}}
GetActivitiesForActivityPerson

这是递归函数

        private IEnumerable<ProductLineDto> GetGanttDataByPerson(IEnumerable<ProductLineDto> ganttData, GanttFilterDto ganttFilterDataModel)
        {
            if(ganttFilterDataModel.PersonId == null)
                return ganttData;

            var gdata = ganttData.CopyProductLineDtosEnumerable().ToList();

            for(var i = 0; i < gdata.Count; i++)
            {
                    var pType = gdata[i].ProjectType.CopyProjectTypeDtoEnumerable().ToList();

                    for (var j = 0; j < pType.Count; j++)
                    {
                        var projects = pType[j].Projects.CopyProjectDtosEnumerable().ToList();
                        for (var a = 0; a < projects.Count; a++)
                        {
                            // projects[a].Children is not being modified
                            projects[a].Children = GetActivitiesForActivityPerson(projects[a].Children, ganttFilterDataModel.PersonId);
                        }
                        pType[j].Projects = projects;
                    }

                gdata[i].ProjectType = pType;
            }

            ganttData = gdata;
            return ganttData; ;
        }

当我单步执行递归函数时, private IEnumerable<SubProjectDto> GetActivitiesForActivityPerson(IEnumerable<SubProjectDto> children, Guid? personId) { var subProjects = children.CopySubProjectDtoEnumerable().ToList(); for (var i = 0; i < subProjects.Count; i++) { var acts = subProjects[i].Activities.CopyActivityDtoEnumerable().ToList(); acts.RemoveAll(activity => activity.ActivityPersons.All(person => person.PersonID != personId)); subProjects[i].Activities = acts; if(subProjects[i].Children.Any()) GetActivitiesForActivityPerson(subProjects[i].Children, personId); } children = subProjects; return children; } 语句由修改后的return children组成。但是,这行代码不包含修改后的activities,它由赋值调用后的原始数据集组成。

activities

如果有人想知道所有复制方法是什么样的,他们只会返回projects[a].Children = GetActivitiesForActivityPerson(projects[a].Children, ganttFilterDataModel.PersonId);的新实例。

例如

DTO

1 个答案:

答案 0 :(得分:2)

为了将来参考,请提交可执行的代码摘录。为了获得最佳效果,您希望人们能够将问题代码直接复制到他们的IDE中进行播放。

此外,代码令人痛苦。它简化了这个:

using System;
using System.Collections.Generic;
using System.Linq;

namespace StackOverflow_LoopMadness
{
    class Program
    {
        static void Main(string[] args)
        {
            // ???
        }

        private static IEnumerable<ProductLineDto> GetGanttDataByPerson(IEnumerable<ProductLineDto> ganttData, GanttFilterDto ganttFilterDataModel)
        {
            if (ganttFilterDataModel.PersonId == null)
                return ganttData;

            foreach (var pType in ganttData)
            {
                foreach (var project in pType.Projects)
                { 
                    project.Children = GetActivitiesForActivityPerson(project.Children, ganttFilterDataModel.PersonId);
                }
            }
            return ganttData;
        }

        private static IEnumerable<SubProjectDto> GetActivitiesForActivityPerson(IEnumerable<SubProjectDto> children, Guid? personId)
        {
            foreach (var subProject in children)
            {
                subProject.Activities = subProject.Activities.Where(a => a.ActivityPersons.All(person => person.PersonID != personId));

                if (subProject.Children.Any())
                {
                    GetActivitiesForActivityPerson(subProject.Children, personId);
                }
            }
            return children;
        }
    }

    class SubProjectDto
    {
        public IEnumerable<Activity> Activities { get; internal set; }
        public IEnumerable<SubProjectDto> Children { get; internal set; }
    }

    class Activity
    {
        public IList<ActivityPerson> ActivityPersons { get; internal set; }
    }

    class ActivityPerson
    {
        public Guid? PersonID { get; internal set; }
    }

    class GanttFilterDto
    {
        public Guid PersonId { get; internal set; }
    }

    class ProductLineDto
    {
        public IList<Project> Projects { get; internal set; }
    }

    class Project
    {
        public IEnumerable<SubProjectDto> Children { get; set; }
    }
}

正如您所看到的,使用foreach循环交换for循环会大大减少代码量并提高可读性。删除了无关的副本列表。而且,我需要通过猜测来创建6个数据类才能编译。

我放弃了为这个程序找出合适的驱动程序。但是,我确信修改嵌套元素的问题已经解决,因为编辑的变量只能通过引用传递。

我认为您的问题源于对传递值与参考值的不完全理解,因此您对复制品的使用造成了混乱。微软可以比我更好地解释它,所以请查看这个link