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 Query
- 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
More Query from same tag
- how to get an ordered list with default values using linq
- Materialising "Friendly" Entities from a disconnected EF5 Context
- Update records with Entity Framework without loading in first
- Controls null in Page_Load and Page_PreRenderComplete
- Azure IListBlobItem Oftype CloudBlob Linq Change List<CloudBlob>
- LINQ - Nested Query
- LINQ to CRM - OR in Where clause
- Linq - Question concerning the 'Any' method
- How to count sets of specific characters in a string
- C# GroupBy - Creating multiple grouping levels
- Hitting the 2100 parameter limit (SQL Server) when using Contains()
- Linq preprocessor?
- .net IEnumerable Except with custom IEqualityComparer not working as expected
- Creating Key/Value collections using LINQ To Objects
- Read Particular XML Node (values, not just InnerText) AND put the values in TextBox
- Parsing XML with Linq with multiple descendants
- Workaround for DefaultIfEmpty
- Building a method using lambda expressions call
- Deferred execution vs ToList gives different results
- Use LINQ to group by one field, sum based on group AND store on Dictionary
- Not expected result when use Linq Select
- Enumeration Type exception when attempting to filter a LINQ query
- Check if string value is a word
- Is Order of items read from a XDocument, by LINQ, Guaranteed?
- How can i get the manufacturers from all products?
- C#/VB.Net How to collapse specific values in a list
- How can I use LINQ, to Sort and Filter items in a List<ReturnItem> collection?
- Override lazy loading on anonymous type with LINQ to entity
- Result of linq query can be used as reference?
- C# Remove object from list stored in an ASP session