I’m sure there’s a better way to do this but I don’t know how. Could I use what they call reflection?
I want to order the resultList by the value in id where the id has the same value as the name of the property to order by.
var resultList = repository.GetPersons();
// Order the result based on the value in the string id
if (id == "organisation")
resultList = resultList.OrderBy(p => p.organisation);
else if (id == "surname")
resultList = resultList.OrderBy(p => p.surname);
else if (id == "lastname")
resultList = resultList.OrderBy(p => p.lastname);
else if (id == "adress")
resultList = resultList.OrderBy(p => p.adress);
}
This will surely helps you : Dynamic LINQ OrderBy using String Names!
Class
Use