LoJo
LoJo

Reputation: 142

Explaination for "To static method invocation" ReSharper suggestion

Can someone help me understand the ReSharper suggestion to change:

var progs = ctx.Programs.Where(i => progIds.Contains(i.ID) && i.Projects.Any())

to this:

var progs = Queryable.Where
       (ctx.Programs, i => progIds.Contains(i.ID) && Enumerable.Any<Project>(i.Projects))

(Idea is to return programs that match a list of ids, progIds, and contain projects, a navigational property)

Or this:

GridView gv = new GridView();
gv.DataSource = ctx.Programs.Where(i => progIds.Contains(i.ID)).ToList();

to this:

GridView gv = new GridView();
gv.DataSource = Queryable.Where(ctx.Programs, i => progIds.Contains(i.ID)).ToList();

I'm specifically wondering about the benefits of this. I'm currently trying to reduce memory allocation where possible

Upvotes: 2

Views: 196

Answers (2)

Jakub Lortz
Jakub Lortz

Reputation: 14904

Queryable.Where is an extension method. It is defined as

public static IQueryable<TSource> Where<TSource>
            (this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)

You can call it as a regular static method, passing the IQueryable as parameter, or as an extension method. The compiler will generate exactly the same code.

The extension method syntax is more readable than the static method syntax.

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 191058

There are no difference between the cases. That is actually what happens with an extension method under the covers. It is a matter of personal preference, although direct invocation is less commonly used.

Upvotes: 1

Related Questions