Question

As the title reads, is there a way using the ReSharper search pattern to find all instances of .First() that contains a condition, ignoring .First()?

The motivation for this search is in response to question
Why is LINQ .Where(predicate).First() faster than .First(predicate)?

We would like to see how many times we're using First() with a condition. The search will be extended to look for .FirstOrDefault().

Was it helpful?

Solution

You create a pattern for this about the way you would expect. Go to ReSharper > Find > Search with Pattern, and enter this pattern:

$enumerable$.First($args$)

Then in the placeholders list on the right, create two placeholders:

  • Add Placeholder > Expression, and name it enumerable. Don't specify a type (I tried specifying the type as IEnumerable<out T> and descendants, but for some reason that failed to find any usages; but if you don't specify a type, it works).
  • Add Placeholder > Argument, name it args, check the box for "Limit minimal number of arguments", and leave the number set to 1.

I tested this, and it found list.First(i => i%2 == 0) but not list.First(), so seems like exactly what you're asking for.

OTHER TIPS

You could just use VisualStudio Find and use the RegEx option

Something like: .First(\({.+}\));

enter image description here

And you can also use Find and Replace to change all your predicates to Where +First`

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top