score:2
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.
score:6
Get rid of the ToList(). You need an IQueryable not an IList
var FPaged = new PagedList(query, pageIndex, pageSize);
score:10
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);
Source: stackoverflow.com
Related Articles
- Error converting LINQ anonymous type to IList<>
- nested Linq Query Returned an error at run time(Unable to create a constant value of type Anonymous type)
- Linq GroupBy anonymous type error MVC4
- LINQ entity data model generated code error - The type 'DBContexts.Category' already contains a definition for 'ID'
- error when anonymous type is used in join query in LINQ
- Linq Query return Anonymous Type Error
- Anonymous type error when using JOIN in LINQ
- Anonymous Type Error in Linq using Group, Sum, and selecting multiple fields of data after reading in spreadsheet with LinqToExcel
- How to create LINQ Expression Tree to select an anonymous type
- How to return anonymous type from c# method that uses LINQ to SQL
- Linq error generic parameter or the query must use a nullable type
- LINQ select query with Anonymous type and user Defined type
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- Invalid anonymous type member declarator in LINQ
- How to assign a nullable int property in an anonymous type in LINQ with a Union?
- group by using anonymous type in Linq
- Error message "Operator '.' cannot be applied to operand of type 'lambda expression'" when converting a method to Extension Method?
- Anonymous Type Appears in both Error
- C# LINQ build expression with anonymous type
- Anonymous Type with Linq and Guid
- C# Linq Projecting Anonymous type onto an interface
- LINQ query with GROUP BY and Count(*) into Anonymous Type
- Join anonymous type in LINQ
- LINQ - Contains with anonymous type
- Convert KeyValuePair to anonymous type in a LINQ query
- How to Select by LINQ into anonymous type in F#
- LINQ query returns error "The expected type was 'System.Int32' but the actual value was null."
- linq query anonymous type cannot be converted to POCO object
- Using a type alias in a linq statement is generating an error
- LINQ Source Code Available
- C# linq to merge multidimentional string array
- C# Linq GroupBy
- Convert IQueryable<T> or LINQ query to unique string
- Lambda expression not correct - need to return a boolean Func
- cosmosdb where clause in sub lists with linq
- linq contains string comparison. IEqualityComparer
- How compose Expression: selector + predicate?
- (EF) Update related data on insert new entity
- C# LINQ/Lambda scope question
- EFCore query to get amount of products for each vendor by product search term
- How to get an IDS list of a Json type object with LINQ
- LINQ Filter Nth Level Nested list
- Linq to sql ! operator for nullable of type int
- AspNetBoilerplate Returning all records depite where clause
- How to create "inline if statement" with expressions in dynamic select for null checking
- How to get extreme leaf node against heights parent in self referencing linq
- Value for XML Root Element is completely wrong, not finding child nodes
- Join dynamic query result with an EntitySet using Linq?
- How express SQL left join with IS NULL in LINQ
- Get indices of top x equal elements