Inspection Option Suggestion - Possible multiple enumeration of IEnumerable

I have a suggested addition to the Possible multiple enumeration of IEnumerable inspection option in ReSharper.  ReSharper can understand the possible multiple enumeration when you explicitly access an IEnumerable twice, such as in the example on

IEnumerable<string> names = GetNames();

foreach (var name in names)

  Console.WriteLine("Found " + name);

var allNames = new StringBuilder();

foreach (var name in names)

  allNames.Append(name + " ");

However, ReSharper misses the case when you use an IEnumerable inside a LINQ statement.  As an example:

IEnumerable<Employee> employees = GetEmployees();

IEnumerable<Team> teams = GetTeams().Where(t => t.Enabled);
return employees.Where(e => teams.Any(t => t.TeamID == e.TeamID && t.Name == "Engineering"));

ReSharper 8.2.2 doesn't give an inspection notification to me for this code.  But in this case, for every employee, we filter on enabled teams.  Adding a ToList() or .ToArray() to the end of the 2nd line may dramatically speed up this code block.

I have spent countless hours dealing with these cases, trying to speed up my code and I think it would help if ReSharper found this for me.


Please sign in to leave a comment.