My repository method fetches stuff from the database. It accepts the sort order as an argument:
IEnumerable<Car> getCars<TSortKey>(Expression<Func<Car, TSortKey>> sort);
I use TSortKey
, because I won't know which property will be used until runtime, it could be x => x.Name
or x => x.Make
which are strings, but it could also be x => x.History.Age
which is an integer.
The user chooses the sort order, then I set up the sort predicate in a switch and call into that method.
Expression<Func<Car, object>> sortPredicate;
switch (sortOption) {
case SortOption.Name: sortPredicate = s => s.Name; break;
case SortOption.Make: sortPredicate = s => s.Make; break;
case SortOption.Age: sortPredicate = s => s.History.Age; break;
default: sortPredicate = s => s.Name; break;
}
var cars = repo.getCars(sortPredicate);
I use object
in the predicate, as I won't know the type until runtime. But that generates the wrong SQL, and throws.
So how can I fix this?