0

I have a linq query of objects of a known type and I need to sort it based on a property of the object. The problem is I only know the property name at runtime and as a string.

ex.

IQueryable<Thing> thing = from t in ctx.Things select t;
t.OrderBy(t => t.Name);   // This would work if I knew the property at compile time...
t.OrderBy("Name asc");    // But this is what I really need!  and it doesn't work.

Is there a way to do this for LINQ to Entities? I'm using entity framework 4.1

codemonkey
  • 1,213
  • 2
  • 14
  • 31

1 Answers1

-1

There are two

  1. .OrderBy(m => m.Name)
  2. .OrderByDescending(m => m.Name)

First one represents Ascending, the other one descending.

Hope it helps.

What about try this one ! Let's see if helps

    Expression orderByProperty = Expression.Property(prm, sortBy);

    // get the paged records
    IQueryable<PostingListItemDto> query = be.buskerPosting
    .Where(posting => posting.buskerAccount.cmsMember.nodeId == m.Id)
    .OrderBy(orderByExpression)
Tun Hein
  • 1
  • 2