I think I understand what you are asking for here. You want to build a LINQ query from string arguments
Good. I like the challenge.
IComparable GetPropValue( object src, string propName ) { return (IComparable)src.GetType( ).GetProperty( propName ).GetValue( src, null ); } IEnumerable<Premise> SortMyPremises(IEnumerable<Premise> premises, string propertyName, string ascendingOrDescending) { return ascendingOrDescending = "ascending" ? premises.OrderBy(p => GetPropValue(p, propertyName)) : premises.OrderByDescending(p => GetPropValue(p, propertyName)); }
The reason you wrote it does not work is because the LINQ expression is converted to code at compile time, and the line you pass in is not evaluated until runtime.
source share