Create LINQ to entities OrderBy expression on the fly
Posted
by AyKarsi
on Stack Overflow
See other posts from Stack Overflow
or by AyKarsi
Published on 2010-05-15T20:04:11Z
Indexed on
2010/05/15
20:14 UTC
Read the original article
Hit count: 261
linq-to-entities
|c#
I'm trying to add the orderby expression on the fly. But when the query below is executed I get the following exception:
System.NotSupportedException: Unable to create a constant value of type 'Closure type'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
The strange thing is, I am query exactly those primitive types only.
string sortBy = HttpContext.Current.Request.QueryString["sidx"];
ParameterExpression prm = Expression.Parameter(typeof(buskerPosting), "posting");
Expression orderByProperty = Expression.Property(prm, sortBy);
// get the paged records
IQueryable<PostingListItemDto> query =
(from posting in be.buskerPosting
where posting.buskerAccount.cmsMember.nodeId == m.Id
orderby orderByProperty
//orderby posting.Created
select new PostingListItemDto { Set = posting }).Skip<PostingListItemDto>((page - 1) * pageSize).Take<PostingListItemDto>(pageSize);
Hope somebody can shed some light on this!
© Stack Overflow or respective owner