Create LINQ to entities OrderBy expression on the fly
- by AyKarsi
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!