Sunday 2 July 2017

Error converting LINQ anonymous type to IList<>


2down voteaccepted
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.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.