user2062383
user2062383

Reputation: 959

Possible Multiple Enumeration of IEnumerable while checking for children/grandchildren objects

I have the following query:

baseQuery = Buildings.Where(
     i => i.Apartment != null 
     && i.Apartment.Manager != null 
     && i.Apartment.Manager.Name == Username);

I think I'm getting the error because I'm performing three separate queries here, so this operation is going to be super slow. Any way I can optimize this check?

Edit: I get "Possible Multiple Enumeration of IEnumerable" from resharper.

Upvotes: 0

Views: 206

Answers (1)

Richard
Richard

Reputation: 30628

Assuming that ContextProvider.Context.Inspections is a DbSet or an IQueryable, this will not result in multiple enumerations of an IEnumerable. The expression will be translated into an SQL query and executed on the server. You can check this by putting a breakpoint after your line, and hovering over baseQuery to see the generated SQL.

Looks like a false positive from resharper.

Upvotes: 2

Related Questions