带有字符串 keySelector 的 OrderBy
我有以下函数,它根据对象的属性提取不同的值,这里是 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; }
}
我这样做是因为我不知道之前需要提取哪些属性值。 它正在工作,只是结果未排序。
您能帮我纠正排序以使 OrderBy 按预期工作吗?
属性是字符串,我不需要链接排序。我也不需要指定排序顺序。
预先非常感谢, 约翰.
I have the following function that is extracting me distinct 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'm doing this because I do not know in before which property values I'll need to extract.
It's working, just the result is not sorted.
Can you please help me correct the sorting to make the OrderBy work as expected?
The properties are strings and I don't need to chain the sorting. I don't need to specify the sorting order either.
Thanks a lot in advance,
John.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
keySelector
目前为每个返回相同字符串(属性名称);由于 LINQ 通常是一种稳定的排序,因此不会导致整体变化。由于您已经投影到字符串值,因此您可以在此处简单地使用一个简单的x=>x
映射:按项目本身进行排序。
Your
keySelector
currently returns the same string for each (the property name); and since LINQ is typically a stable sort, this results in no overall change. Since you have already projected to the string values, you can simply use a trivialx=>x
mapping here:to order by the items themselves.
感谢您提供优雅的解决方案。我进一步扩展了 CreateSelectorExpression 方法,以便可以在上面示例中的 Client 类之外利用它。
用法
Thanks for the elegant solution. I further expanded upon the CreateSelectorExpression method so it can be leveraged outside of the Client class in the example above.
Usage