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);
}
/// 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\$