Created
June 29, 2018 12:59
-
-
Save Liminiens/27183478d62675ec195c500e00c1888b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class UntypedOrderBy | |
{ | |
private static readonly ConcurrentDictionary<(Type, string), Delegate> Lambdas = new ConcurrentDictionary<(Type, string), Delegate>(); | |
public static IEnumerable<TType> OrderyByProperty<TType>(this IEnumerable<TType> collection, string name) | |
{ | |
var lambda = Lambdas.GetOrAdd((typeof(TType), name), key => | |
{ | |
ParameterExpression entity = Expression.Parameter(typeof(TType)); | |
MethodCallExpression getterCall = | |
Expression.Call(entity, typeof(TType).GetProperty(name).GetGetMethod()); | |
UnaryExpression castToObject = Expression.Convert(getterCall, typeof(object)); | |
return Expression.Lambda(castToObject, entity).Compile(); | |
}); | |
return collection.OrderBy((Func<TType, object>)lambda); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment