1

My issue is that every time I load this view my application sends 249 identical queries to the database. Originally I was using lazy loading and the number of queries was double.

The 249 number above represents the number of rows this query returns.

My understanding is that .Include creates a join which should eliminate this behavior?

Can anyone tell me how to eliminate this duplication of queries?

Cheers!

The code below is pseudo code and is not intended to compile.

Controller:

 var apples = _unitOfWork.Context.Apples
    .Include(x=> x.AppleTypes)
    .OrderByDescending(x => x.Id)
    .Where(x => x.Status == (int)AppleStatusConstants.New 
          && x.IsRejected != true && x.AppleManId != null);

 return View(apples);

View:

@model IEnumerable<Apple>

@Html.DisplayNameFor(model => model.AppleTypes.TypeSeason)
@foreach (var item in Model){
         @Html.DisplayFor(modelItem => item.AppleTypes.TypeSeason)
         }

SQL Trace from Glimpse:

    SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[Type] AS [Type],  
[Extent2].[Id] AS [Id1], 
[Extent2].[TypeSeason] AS [TypeSeason], 

FROM  [dbo].[Apples] AS [Extent1]
LEFT OUTER JOIN [dbo].[AppleTypes] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id]
WHERE (0 = [Extent1].[Status]) AND ( NOT ((1 = [Extent1].[IsRejected]) AND ([Extent1].[IsRejected] IS NOT NULL))) AND ([Extent1].[OrgUnitId] IS NOT NULL)
ORDER BY [Extent1].[Id] DESC

Glimpse Screenshot enter image description here

1 Answer 1

1

Multiple DB queries when Eager Loading in Entity Framework 6

You haven't told it to Eager Load the base query, only it's Includes.

This code:

var apples = _unitOfWork.Context.Apples
  .Include(x=> x.AppleTypes)
  .OrderByDescending(x => x.Id)
  .Where(x => x.Status == (int)AppleStatusConstants.New 
         && x.IsRejected != true && x.AppleManId != null);

Is creating an IQueryable< T >. The simple solution is to add .ToList() at the end.

  .Where(x => x.Status == (int)AppleStatusConstants.New 
     && x.IsRejected != true && x.AppleManId != null)
  .ToList(); 

So in your code, this is the culprit:

@foreach (var item in Model){

That is causes EF to query for one entity at a time.

PS: The following Statements are identical (unless you've overridden object.cshtml)

@foreach (var item in Model){
  @Html.DisplayFor(modelItem => item.AppleTypes.TypeSeason)
}

and

@Html.DisplayFor(m => m)

and

@Html.DisplayForModel()
Sign up to request clarification or add additional context in comments.

2 Comments

Is .ToList() a more expensive operation? One of the senior programmers here encourages me not to user .ToList() whenever possible
There is no good answer. It simply depends. There are times when .ToList() is the correct solution and times when it is not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.