I have a snippet of code here that uses C# and Linq. The ganttData variable I have has 12 warnings stating:

possible multiple enumerations of IENumerable

Should I change it to a List or leave it the way it is?

private IEnumerable<ProductLineDto> FilterAtHighestLevel(IEnumerable<ProductLineDto> ganttData, GanttFilterDto ganttFilterDataModel)
            if (ganttFilterDataModel.ProjectId == null)
                return ganttData;
            //Check if it's a productLine, project, or subproject that was selected from the project filter
            ProductLine productLine = UnitOfWork.ProductLineRepository.GetAll().SingleOrDefault(x => x.ProductLineID == ganttFilterDataModel.ProjectId.Value);
            Project project = UnitOfWork.ProjectRepository.GetAll().SingleOrDefault(x => x.ProjectID == ganttFilterDataModel.ProjectId.Value);
            SubProject subProject = UnitOfWork.SubProjectRepository.GetAll().SingleOrDefault(x => x.SubProjectID == ganttFilterDataModel.ProjectId.Value);

            if (productLine != null)
                ganttData = GetProuctLinesFromDto(ganttData, ganttFilterDataModel.ProjectId.Value);
            else if (project != null)
                ganttData = GetProuctLinesFromDto(ganttData, project.ProductLineID);
                //its always the first one
                ganttData.ElementAt(0).Projects = ganttData.ElementAt(0).Projects.Where(x => x.ProjectId == ganttFilterDataModel.ProjectId.Value);
            else if (subProject != null)
                ganttData = GetProuctLinesFromDto(ganttData, subProject.Project.ProductLineID);

                /// Don't just filter for the subproject get the project and show the entire project tree
                /// What's the point of this filter then?

                var rootProject = SubProjectService.EntityRepo.GetAll().FirstOrDefault(x => x.SubProjectID == ganttFilterDataModel.ProjectId.Value);
                ganttData.ElementAt(0).Projects = ganttData.ElementAt(0).Projects = ganttData.ElementAt(0).Projects.Where(x => x.ProjectId == rootProject.ProjectID);

            return ganttData;

        private IEnumerable<ProductLineDto> GetProuctLinesFromDto(IEnumerable<ProductLineDto> ganttData, Guid id)
            return ganttData.Where(x => x.ProductLineId == id);
  • \$\begingroup\$ /// What's the point of this filter then? ;-) are you asking us this question? This is not how CR works. You should explain your code to us, not the other way around. \$\endgroup\$
    – t3chb0t
    Commented Feb 5, 2017 at 17:56
  • \$\begingroup\$ @t3chb0t No that was a comment for me and the requirements lol \$\endgroup\$
    – Train
    Commented Feb 5, 2017 at 18:19

1 Answer 1


possible multiple enumerations of IENuemerable

Should I change it to a List or leave it the way it is?

This depends on the situation. If there is an EF query behind the IEnumerable and you have never called .ToList() on it (not even once), then every time you enumerate it, the query will be executed against the database. If this is your case then you need to call .ToList() so your query can be executed and the results can be stored in a list on the application side. Whenever you want to get a fresh copy from the database, then you can run your query again or run a query which gets you the delta.

If you are doing everything in memory and there are no database calls, then if items are added to the underlying list, you will not get them. Consider this snippet:

IEnumerable<int> list = new List<int> { 1, 2 };
list.Count(); // returns 2


list.Count(); // it still returns 2 
  • 4
    \$\begingroup\$ list.ToList().Add(3); you are not adding to the underlying list you are creating a new list with ToList() and adding new values \$\endgroup\$ Commented Apr 25, 2018 at 9:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.