Warning when a linq query will be evaluated more than once?

[v4.5]
When I use Linq queries the evaluated result is often static. But in the code the result query is used several times, causing to be evaluated every time I iterate it.

Is it possible to add a feature that allows me to show this as a e.g. a warning?

Example with lambda:

void Foo(IEnumerable<int> sources)
{
  var positiveNumbers = sources.Where(x => x>0);

   foreach(var ones in positiveNumbers.Where(x => x==1) )
    ...

   foreach(var others in positiveNumbers.Where(x => x!=1) )
    ...
}

Here I guess I would be better off evaluating the positiveNumbers list only 1 time, e.g. by using
sources.Where(x => x>0).ToList();
and then iterating the list.

Does this sound like a reasonable feature?

Thanks.

1 comment
Comment actions Permalink

I've created a feature request: http://youtrack.jetbrains.net/issue/RSRP-178868

Andrey Serebryansky

Support Engineer

JetBrains, Inc

http://www.jetbrains.com

"Develop with pleasure!"
0

Please sign in to leave a comment.