Multiple enumerations of IEnumerable 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&lt;ProductLineDto&gt; FilterAtHighestLevel(IEnumerable&lt;ProductLineDto&gt; 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 =&gt; x.ProductLineID == ganttFilterDataModel.ProjectId.Value);
            Project project = UnitOfWork.ProjectRepository.GetAll().SingleOrDefault(x =&gt; x.ProjectID == ganttFilterDataModel.ProjectId.Value);
            SubProject subProject = UnitOfWork.SubProjectRepository.GetAll().SingleOrDefault(x =&gt; 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 =&gt; 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 =&gt; x.SubProjectID == ganttFilterDataModel.ProjectId.Value);
                ganttData.ElementAt(0).Projects = ganttData.ElementAt(0).Projects = ganttData.ElementAt(0).Projects.Where(x =&gt; x.ProjectId == rootProject.ProjectID);

            }
            return ganttData;
        }

        private IEnumerable&lt;ProductLineDto&gt; GetProuctLinesFromDto(IEnumerable&lt;ProductLineDto&gt; ganttData, Guid id)
        {
            return ganttData.Where(x =&gt; x.ProductLineId == id);
        }
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&lt;int&gt; list = new List&lt;int&gt; { 1, 2 };
list.Count(); // returns 2

list.ToList().Add(3);
list.ToList().Add(4);

list.Count(); // it still returns 2