This project is read-only.

Consider include ItemsCount property in the interface IGridPager

Jun 25, 2014 at 1:17 PM

Problem

If we call Model.GridItems.Count() an SQL query is unnecessarily executed.
@*:-( This triggers a new query!*@
<div class="count-elements">@Model.GridItems.Count()</div>

Solution

If IGridPager made public the GridPager's ItemsCount property, getting the total item count would be much easier and perform much better.
@*:-) This is an in-memory data!*@
<div class="count-elements">@Model.Pager.ItemsCount</div>

Workaround

@{
    var gridPager = (GridMvc.Pagination.GridPager)Model.Pager;
}

<div>Total: @gridPager.ItemsCount</div>
Jun 25, 2014 at 3:49 PM
Hi,

GridPager also call an SQL Count() query

GridPager.cs: 120
        public virtual void Initialize<T>(IQueryable<T> items)
        {
            ItemsCount = items.Count(); //take total items count from collection
        }
Now IGridPager does not contain ItemsCount property, because you can override it and do not perform COUNT() query, if you don't need them - http://gridmvc.azurewebsites.net/Home/AjaxPaging
Jun 25, 2014 at 4:21 PM
Bukharin wrote:
Hi,

GridPager also call an SQL Count() query

GridPager.cs: 120
        public virtual void Initialize<T>(IQueryable<T> items)
        {
            ItemsCount = items.Count(); //take total items count from collection
        }
Now IGridPager does not contain ItemsCount property, because you can override it and do not perform COUNT() query, if you don't need them - http://gridmvc.azurewebsites.net/Home/AjaxPaging
Hi Bukharin!

Thank you for your attention. Please, notice that this piece of code executes 2 SQL Counts:
@model GridMvc.Grid<RevistaSuscripcion>
·
·
·
<div class="container">
    <div class="grid-title">
        @Resources.RevistasSuscripcion
    </div>

    @Html.Grid(Model).Columns(columns =>
    {
        columns.Add(r => r.Titulo)
            .Titled(@Resources.Titulo)
            .Encoded(false)
            .Sanitized(false)
            .RenderValueAs(r => Html.ActionLink(r.Titulo, "Details", "Revistas", new { id = r.ColeccionId }, null))
            .SortInitialDirection(GridMvc.Sorting.GridSortDirection.Ascending);
        columns.Add(r => r.Suscrito)
            .Titled("Suscrito")
            .Encoded(false)
            .Sanitized(false)
            .Filterable(true);
    }).Sortable().WithPaging(10).SetRowCssClasses(s => s.Suscrito ? "highlight" : string.Empty)
</div>
@*:( This triggers a new sql Count!*@
<div class="count-elements">@Model.GridItems.Count()</div>
Whereas this one reuses the first count so that only one sql query gets triggered...
@model GridMvc.Grid<RevistaSuscripcion>
·
·
·
<div class="container">
    <div class="grid-title">
        @Resources.RevistasSuscripcion
    </div>

    @Html.Grid(Model).Columns(columns =>
    {
        columns.Add(r => r.Titulo)
            .Titled(@Resources.Titulo)
            .Encoded(false)
            .Sanitized(false)
            .RenderValueAs(r => Html.ActionLink(r.Titulo, "Details", "Revistas", new { id = r.ColeccionId }, null))
            .SortInitialDirection(GridMvc.Sorting.GridSortDirection.Ascending);
        columns.Add(r => r.Suscrito)
            .Titled("Suscrito")
            .Encoded(false)
            .Sanitized(false)
            .Filterable(true);
    }).Sortable().WithPaging(10).SetRowCssClasses(s => s.Suscrito ? "highlight" : string.Empty)
</div>
@{
    var gridPager = (GridMvc.Pagination.GridPager)Model.Pager;
}

<div>Total: @gridPager.ItemsCount</div>
Jun 26, 2014 at 5:52 PM
You right.

It's a bit strange to use a pager to retrieve items count. But grid does not need this info (to make an +1 db query). If you have a suggestion for better way to retrieve items count, I'll be glad to hear it.
Jun 27, 2014 at 11:43 AM
Edited Jun 27, 2014 at 11:43 AM
I see that this code on GridBase.cs has been commented out most likely for good reason.
/// <summary>
///     Total count of items in the grid
/// </summary>
//public int ItemsCount
//{
//    get
//    {
//        if (_itemsCount < 0)
//            _itemsCount = GridItems.Count();
//        return _itemsCount;
//    }
//    set { _itemsCount = value; //value can be set by pager (for minimizing db calls)
//    }
//}
What I was going to suggest was even easier:

Grid.cs

public int ItemsCount
{
    get
    {
        return Pager.ItemsCount;
    }
}