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 IENumerableShould 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);        }
Multiple enumerations of IEnumerable
c#;linq
possible multiple enumerations of IENuemerableShould 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 2list.ToList().Add(3);list.ToList().Add(4);list.Count(); // it still returns 2 