I have the following function that retrieves me various values ββbased on the properties of an object, here Client.
public List<DistinctValue> GetDistinctValues(string propertyName) { //how should I specify the keySelector ? Func<string, object> keySelector = item => propertyName; var list = new List<DistinctValue>(); var values = this.ObjectContext.Clients.Select(CreateSelectorExpression (propertyName)).Distinct().OrderBy(keySelector); int i = 0; foreach (var value in values) { list.Add(new DistinctValue() { ID = i, Value = value }); i++; } return list; } private static Expression<Func<Client, string>> CreateSelectorExpression (string propertyName) { var paramterExpression = Expression.Parameter(typeof(Client)); return (Expression<Func<Client, string>>)Expression.Lambda( Expression.PropertyOrField(paramterExpression, propertyName), paramterExpression); } public class DistinctValue { [Key] public int ID { get; set; } public string Value { get; set; } }
I do this because I donβt know to what property values ββI will need to extract. It works, the result is not sorted.
Could you help me fix the sorting to make OrderBy work as expected?
Properties are strings, and I don't need to bind the sort. I also do not need to specify sort order.
Thanks a lot in advance, John.
source share