3
\$\begingroup\$

I have a recursive statement that removesactivities that don't have a resource type for a certain OrganizationID. I was wondering if there was a better way to do this where I can eliminate some of the for loops and use a little more linq

 private IEnumerable<SubProjectDto> GetProjectAndActivitiesForByOrg(IEnumerable<SubProjectDto> children, Guid? organiazaitonId)
 {
 var subProjects = new List<SubProjectDto>();
 subProjects.AddRange(children);
 for (var i = 0; i < subProjects.Count; i++)
 {
 var activities = new List<ActivityDto>();
 activities.AddRange(subProjects[i].Activities);
 for (var j = 0; j < activities.Count; j++)
 {
 var resourceTypesForOrgs = activities[j].ActivityPersons.Where(x => x.ResourceType.OrganizationId == organiazaitonId).Count();
 if (resourceTypesForOrgs == 0)
 activities.Remove(activities[j]);
 }
 subProjects[i].Activities = activities;
 if (subProjects[i].Children.Any())
 GetProjectAndActivitiesForByOrg(subProjects[i].Children, organiazaitonId);
 }
 children = subProjects;
 return children;
 }
t3chb0t
44.6k9 gold badges84 silver badges190 bronze badges
asked Mar 31, 2017 at 15:31
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Here's my attempt. I assume, LINQ-method based approach is allowed by your problem statement.

I have not tested the code, so please cover it by unit test(s) which you may already have. Also, please notice that this is not a pure function in the functional sense of this term. It means, the method changes the state of the arguments passed in (which may or may not be desirable in different scenarios).

private IEnumerable<SubProjectDto> GetProjectAndActivitiesForByOrg(IEnumerable<SubProjectDto> children, Guid? organiazaitonId)
{
 return children.Select(childSubProject =>
 {
 childSubProject.Activities.RemoveAll(activity =>
 activity.ActivityPersons.All(person => person.ResourceType.OrganizationId != organiazaitonId));
 // if (childSubProject.Children.Any()) // UPD: This check is not really required
 GetProjectAndActivitiesForByOrg(childSubProject.Children, organiazaitonId);
 return childSubProject;
 });
}
answered Mar 31, 2017 at 16:25
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Thank you.I did have to make a slight adjustment but this worked quite well! \$\endgroup\$ Commented Mar 31, 2017 at 16:38
  • 1
    \$\begingroup\$ @OrthoHomeDefense actually, you can even remove the line if (childSubProject.Children.Any()) since childSubProject.Children is guaranteed to be not null as I understand. That will make it one line shorter... (I updated the answer) \$\endgroup\$ Commented Mar 31, 2017 at 16:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.