Asp.Net MVC - Rob Conery's LazyList - Count() or Count

Posted by Adam on Stack Overflow See other posts from Stack Overflow or by Adam
Published on 2009-02-17T20:38:12Z Indexed on 2010/04/22 13:43 UTC
Read the original article Hit count: 653

I'm trying to create an html table for order logs for customers. A customer is defined as (I've left out a lot of stuff):

public class Customer
{
    public LazyList<Order> Orders { get; set; }
}

The LazyList is set when fetching a Customer:

public Customer GetCustomer(int custID)
{
    Customer c = ...
    c.Orders = new LazyList<Order>(_repository.GetOrders().ByOrderID(custID));
    return c;
}

The order log model:

public class OrderLogTableModel
{
    public OrderLogTableModel(LazyList<Order> orders)
    {
        Orders = orders;
        Page = 0;
        PageSize = 25;
    }

    public LazyList<Order> Orders { get; set; }
    public int Page { get; set; }
    public int PageSize { get; set; }
}

and I pass in the customer.Orders after loading a customer. Now the log i'm trying to make, looks something like:

<table>
<tbody>
<% 
    int rowCount = ViewData.Model.Orders.Count();
    int innerRows = rowCount - (ViewData.Model.Page * ViewData.Model.PageSize);
    foreach (Order order in ViewData.Model.Orders.OrderByDescending(x => x.StartDateTime)
                            .Take(innerRows).OrderBy(x => x.StartDateTime)
                            .Take(ViewData.Model.PageSize))
    {
        %>
        <tr>
            <td>
                <%= order.ID %>
            </td>
        </tr>
        <%
    }
%>
</tbody>
</table>

Which works fine. But the problem is evaluating ViewData.Model.Orders.Count() literally takes about 10 minutes.

I've tried with the ViewData.Model.Orders.Count property instead, and the results are the same - takes forever.

I've also tried calling _repository.GetOrders().ByCustomerID(custID).Count() directly from the view and that executes perfectly within a few ms.

Can anybody see any reason why using the LazyList to get a simple count would take so long? It seems like its trying to iterate through the list when getting a simple count.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about linq-to-sql