Reputation: 13
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);
}
Upvotes: 0
Views: 82
Reputation: 9901
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?
Upvotes: 1