0

my question now : is this code at view realy make lazy loading and each time it hit the database ??

@{
int langId = ViewBag.LangId;

int i = 0;
foreach (var item in Model)
{
    i++;
    <tr class="@(i % 2 == 0 ? "even" : "odd")">
        <td>
            @Html.DisplayFor(modelItem => item.AlbumsLocs.FirstOrDefault(b => b.LanguageId == langId).Title)
        </td>
    </tr>
}

}

my controller code :

public ViewResult Index()
    {
        var moduleItems = db.Albums.Include(x => x.AlbumsLocs).Where(a => a.AlbumVocId == id).ToList();

        return View(moduleItems);
    }
ahmad alaa
  • 13
  • 6
  • 1
    This code is executed at run-time on the server and has nothing to do with your browser performance. However, if this is generating 1000+ table rows `` then Firefox may be affected when rendering the page. – Christopher.Cubells Jan 27 '14 at 19:12

1 Answers1

1

You might be experiencing the "n+1" problem in item.AlbumsLocs.FirstOrDefault(b => b.LanguageId == langId)

Essentially, you are using lazy-loading in your initial request to get the Model. Then for each item in that model you are hitting the database to get the title of the current item.

The solution would be to get the Title in the initial query.

More information on the n+1 problem: What is SELECT N+1?

Community
  • 1
  • 1
Andy T
  • 10,223
  • 5
  • 53
  • 95
  • this is my query at controller : var moduleItems = db.Albums.Include("AlbumsLocs").Where(a => a.AlbumVocId == id); --- and the "AlbumsLocs" at the above example is already included with the model, and i didnt think it go to database every time to get the data ! because the data is already here ! – ahmad alaa Jan 27 '14 at 19:25
  • Hows that affecting Firefox memory consumption? – trailmax Jan 27 '14 at 21:25
  • I do some amendments at code and now it firefox memory not affected, but my question now : is this code at view realy make lazy loading and each time it hit the database ?? : controller code : var moduleItems = db.Albums.Include(x => x.AlbumsLocs).Where(a => a.AlbumVocId == id).ToList(); and i explained the view code above , ill update the question now – ahmad alaa Jan 28 '14 at 08:35