动态 LINQ 帮助排序问题
我有一个简单的 IEnumerable Order 对象集合。我想编写一个通用排序函数,它接受集合和与 Order 对象上的属性相对应的排序键。我不想对每个属性的排序指令进行硬编码。如何构建自动为我生成 LINQ 的动态 LINQ 字符串?这是我的 SortOrders() 函数的当前状态:
public IEnumerable<Order> SortOrders(IEnumerable<Order> orders, string sortKey)
{
// Sort order records
try
{
IEnumerable<Order> sortedOrders = new List<Order>();
// Extract sort key and direction from combined sortKey input
string _sortKey = this.SortKey.Replace(" ASC", "").Replace(" DESC", "");
bool sortASC = this.SortKey.EndsWith(" ASC");
switch (_sortKey)
{
case "CustomerID":
sortedOrders = (sortASC) ? orders.OrderBy(o => o.CustomerID) : orders.OrderByDescending(o => o.CustomerID);
break;
case "CustomerAddress":
sortedOrders = (sortASC) ? orders.OrderBy(o => o.CustomerAddress) : orders.OrderByDescending(o => o.CustomerAddress);
break;
case "CustomerCity":
sortedOrders = (sortASC) ? orders.OrderBy(o => o.CustomerCity) : orders.OrderByDescending(o => o.CustomerCity);
break;
default:
sortedOrders = orders;
break;
}
return sortedOrders;
}
catch
{
return orders; // return original orders list in the event of errors
}
}
我希望
case "CustomerID":
sortedOrders = (sortASC) ? orders.OrderBy(o => o.CustomerID) : orders.OrderByDescending(o => o.CustomerID);
用这样的单个指令替换所有这样的 case 部分:
sortedOrders = (sortASC) ? orders.OrderBy(o => o.PropertyName) : orders.OrderByDescending(o => o.PropertyName);
I have a simple IEnumerable collection of Order objects. I want to write a generic sort function that accepts the collection and a sortkey that corresponds to a property on an Order object. I don't want to hardcode the sort instructions for every property. How can I build a dynamic LINQ string that automatically generates the LINQ for me? Here is my SortOrders() function in its current state:
public IEnumerable<Order> SortOrders(IEnumerable<Order> orders, string sortKey)
{
// Sort order records
try
{
IEnumerable<Order> sortedOrders = new List<Order>();
// Extract sort key and direction from combined sortKey input
string _sortKey = this.SortKey.Replace(" ASC", "").Replace(" DESC", "");
bool sortASC = this.SortKey.EndsWith(" ASC");
switch (_sortKey)
{
case "CustomerID":
sortedOrders = (sortASC) ? orders.OrderBy(o => o.CustomerID) : orders.OrderByDescending(o => o.CustomerID);
break;
case "CustomerAddress":
sortedOrders = (sortASC) ? orders.OrderBy(o => o.CustomerAddress) : orders.OrderByDescending(o => o.CustomerAddress);
break;
case "CustomerCity":
sortedOrders = (sortASC) ? orders.OrderBy(o => o.CustomerCity) : orders.OrderByDescending(o => o.CustomerCity);
break;
default:
sortedOrders = orders;
break;
}
return sortedOrders;
}
catch
{
return orders; // return original orders list in the event of errors
}
}
I am looking to replace all of the case sections like this:
case "CustomerID":
sortedOrders = (sortASC) ? orders.OrderBy(o => o.CustomerID) : orders.OrderByDescending(o => o.CustomerID);
with a single directive like this:
sortedOrders = (sortASC) ? orders.OrderBy(o => o.PropertyName) : orders.OrderByDescending(o => o.PropertyName);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将要排序的表达式作为 Lamba 传递(这是简化/伪代码)
You can pass the expression you want to sort in as a Lamba (this is simplified/pseudocode)
您可以使用 动态 LINQ 并且不需要扩展方法。只需使用 DynamicQueryable 类中的扩展并将键和方向指定为字符串即可。
You can use Dynamic LINQ and there's no need for the extension method. Just use the extension in the DynamicQueryable class and specify both the key and direction as a string.