I have a LINQ to object query to select all the persons that are above 20 years old
IEnumerable<Object> result = null;
result = (from person in AllPersons.ToList()
where person.age > 20
select new
{
FirstName= person.FirstName,
LastName= person.LastName,
Email= person.Email,
PhoneNumber= person.PhoneNumber
});
return result;
I have a parameter string SortProperty I want to use to sort the result based on the property.
So for example if SortProperty="FirstName" I want to sort the result based on the first name.
I tried to do the following:
return result.OrderBy(x => x.GetType().GetProperty(SortProperty));
but it did not work
any idea how to do it?
PS: I don’t want to test all the possibilities, and do a if-else on each, or a case switch. I’m looking for an efficient way to do this
Thanks
Try