【发布时间】:2011-01-11 13:30:55
【问题描述】:
我有以下函数,它根据对象的属性提取不同的值,这里是客户端。
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; }
}
我这样做是因为我不知道之前需要提取哪些属性值。 它正在工作,只是结果没有排序。
您能帮我更正排序以使 OrderBy 按预期工作吗?
属性是字符串,我不需要链接排序。我也不需要指定排序顺序。
提前非常感谢, 约翰。
【问题讨论】: