MvcPaging icon indicating copy to clipboard operation
MvcPaging copied to clipboard

Create an empty paged list

Open wis3guy opened this issue 11 years ago • 6 comments

Sometimes it is useful to be able to create an empty instance of PagedList<T>. As i did not want to change the library, i worked around this by doing this:

public static class PagedList
{
    /// <summary>
    /// Factory method for an empty <see cref="PagedList{T}"/>
    /// </summary>
    /// <typeparam name="T">Type of data contained in the list</typeparam>
    /// <returns>An empty paged list</returns>
    public static IPagedList<T> Empty<T>()
    {
        return new List<T>().ToPagedList(0, 1);
    }
}

and then using

var empty = PagedList.Empty<SomeType>();

it would be cleaner if i could simply do:

var empty = new PagedList<SomeType>();

wis3guy avatar Sep 26 '14 12:09 wis3guy

Sure, it's possible. Just curious: in what situations do you need an empty list?

martijnboland avatar Sep 28 '14 21:09 martijnboland

F.ex when i want to return an empty search result. Sometimes i can already determine that a search is not going to yield any results without firing a query.

Or, f.ex. as a default values on my models. I always want my objects to be in a valid state, which means that in the default constructor i want to set my collection properties to an empty collection.

wis3guy avatar Sep 29 '14 07:09 wis3guy

Alright, clear

martijnboland avatar Sep 29 '14 07:09 martijnboland

Just looking at it, but I think it doesn't make any sense to create an empty constructor, because we'll have to assume some pagesize. Probably better to just use one of the existing constructors:

var emptyList = new PagedList<SomeType>(null, 0, 1);

martijnboland avatar Oct 05 '14 20:10 martijnboland

The idea is not to create a mutable pagedlist, rather an immutable one which indicates no results.

On Sunday, October 5, 2014, Martijn Boland [email protected] wrote:

Just looking at it, but I think it doesn't make any sense to create an empty constructor, because we'll have to assume some pagesize. Probably better to just use one of the existing constructors:

var emptyList = new PagedList<SomeType>(null, 0, 1);

— Reply to this email directly or view it on GitHub https://github.com/martijnboland/MvcPaging/issues/44#issuecomment-57951905 .

Ciao, Geoffrey

Geoffrey Braaf | +31655793290 Freelance .NET Software Architect & Passionate Developer | http://wis3guy.net Findsi: find-as-i | http://www.findsi.com

wis3guy avatar Oct 06 '14 03:10 wis3guy

Thanks for this tip wis3guy. It was indeed helpful.

meatgithub avatar Nov 21 '14 07:11 meatgithub