I'm building a very simple CRUD web-application (ASP.NET MVC) for tennisplayers and the tournaments they can participate.
On a specific page I want to show all tournaments in the database with a title at the top op the page 'All Tournaments' with between brackets the amount of records in the database.
My cshtml would look like this:
The controller for this page is the TournamentController. This controller is using a Manager object which contains the dbcontext. The GetAllTournamentsWithOrgClubAndParticipants() method returns an IEnumerable of tournament objects (with an include of the club and pariticpants, but for my question this is not of any importance).
When I load the page, I see that the same query is fired 3 times. once for the @Model.Count() in the title of the webpage, once for the @Model.Any() to determine wheter or not to show the table and once in the foreach loop. Now I know this is because of defered excecution and I could resolve this by adding a ToList() behind the GetAllTournamentsWithOrgClubAndParticipants() in the controller class, but I often hear NOT TO USE the ToList() method because of this way you are loading everything into memory. But to my feeling for this case it's still better than excecuting the same query 3 times in a row or am I wrong? Any other way I could resolve this?
Thank you very much!
On a specific page I want to show all tournaments in the database with a title at the top op the page 'All Tournaments' with between brackets the amount of records in the database.
My cshtml would look like this:
ASP.net:
@model System.Collections.Generic.IEnumerable<TMS.BL.Domain.Tournament>
@{
ViewBag.Title = "All Tournaments";
Layout = "_Layout";
}
<h3>All Tournaments (@Model.Count())</h3>
@if (!@Model.Any())
{
<p>No tournaments were found...</p>
}
else
{
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Starts</th>
<th scope="col">Ends</th>
<th scope="col">Org. Club</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
@foreach (var t in Model)
{
<tr>
<td>@t.Name</td>
<td>@t.StartDate.ToString("ddd, dd/MM/yyyy")</td>
<td>@t.EndDate.ToString("ddd, dd/MM/yyyy")</td>
<td>@t.OrganizingClub.Name (@t.OrganizingClub.Province - @t.OrganizingClub.Town)</td>
<td>
<a asp-controller="Tournament" asp-action="Details" asp-route-id="@t.Id" class="btn btn-primary btn-sm">Details</a>
</td>
</tr>
}
</tbody>
</table>
}
}
The controller for this page is the TournamentController. This controller is using a Manager object which contains the dbcontext. The GetAllTournamentsWithOrgClubAndParticipants() method returns an IEnumerable of tournament objects (with an include of the club and pariticpants, but for my question this is not of any importance).
C#:
public class TournamentController : Controller
{
private IManager _mgr;
public TournamentController(IManager manager)
{
_mgr = manager;
}
public IActionResult Index()
{
return View(_mgr.GetAllTournamentsWithOrgClubAndParticipants());
}
When I load the page, I see that the same query is fired 3 times. once for the @Model.Count() in the title of the webpage, once for the @Model.Any() to determine wheter or not to show the table and once in the foreach loop. Now I know this is because of defered excecution and I could resolve this by adding a ToList() behind the GetAllTournamentsWithOrgClubAndParticipants() in the controller class, but I often hear NOT TO USE the ToList() method because of this way you are loading everything into memory. But to my feeling for this case it's still better than excecuting the same query 3 times in a row or am I wrong? Any other way I could resolve this?
Thank you very much!