One of the biggest issues here is all of the ToLower()
calls. Strings are immutable, so each change of them (such as changing them to lowercase as seen in your code sample) creates a new object. Additionally, the logic involved in lowercasing a string is more expensive than you might think, because it has to account for things like the current culture settings.
To mitigate the expense, try not altering the string references, instead comparing them with case insensitivity:
var computers = (from DirectoryEntry domain in winDirEntries.Children
where string.Equals(domain.Name, this.Domain, StringComparison.OrdinalIgnoreCase)
from DirectoryEntry pc in domain.Children
where pc.SchemaClassName.IndexOf(Computer, StringComparison.OrdinalIgnoreCase) != -1
select pc.Name).ToList();
Note that I had to change string.Compare
to string.IndexOf
because Compare
does not have an overload that works with case insensitivity.