Accepted answer

pagedlist requires an iqueryable<myquery>. your query object is of type iqueryable<anonymous-type>. to get an iqueryable<myquery> you need to change your select to:

var query = _abc.table
            .join(_def.table, cef => ...etc... })
            .join(_ghi.table, extf => ...etc...})
            .select(jcefn=> new myobject(){ xyz = jcefn....etc...});

you do not need .tolist() to turn this into an iqueryable, it already is.

however, if you do want to execute and cache the iqueriable before passing it to the function, you could do

var cachedquery = query.tolist();
var fpaged = new pagedlist<myobject>(cachedquery.asqueryable<myobject>(), pageindex, pagesize);

in most cases this is not what you want. the pagedlist requires an iqueryable most likely because it will only retrieve the part of the data that is currently needed for a particular page, leaving the rest of the potentially huge dataset behind the query in the database.

but if you actually want to retrieve all the data only once, and then later turn it into a pagedlist, this is the way to go. you can then also reuse the cachedquery in other places, without causing another database retrieval.


get rid of the tolist(). you need an iqueryable not an ilist

var fpaged = new pagedlist(query, pageindex, pagesize);


well yes - query.tolist() will return a list<t>, which doesn't implement iqueryable<t>. indeed, the tolist() would render the paging less useful, as it would all be done locally after fetching the whole table into memory.

just get rid of the call to tolist() and it may well be fine.

edit: okay, if it's not fine because of the anonymous type, adding a tolist call isn't going to help. you either want to use a query which projects to an iqueryable<myobject>, or if you really want a paged query for the anonymous type, you could add an extension method:

public static class pagingextensions
    public static pagedlist<t> paginate<t>(this iqueryable<t> source,
                                           int pageindex, int pagesize)
        return new pagedlist<t>(source, pageindex, pagesize);

then you can use:

// todo: use a more sensible variable name
var fpaged = query.paginate(pageindex, pagesize);

Related Query

More Query from same tag