动态 LINQ 帮助排序问题

发布于 2024-09-28 18:54:27 字数 2043 浏览 6 评论 0原文

我有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明明#如月 2024-10-05 18:54:27

您可以将要排序的表达式作为 Lamba 传递(这是简化/伪代码)

SortOrders(IEnumerable<Order> orders, Func<Order, string> func)
{
    sortedOrders = orders.OrderBy(func) 
    ....                
}


// call:
SortOrders(orders,  o => o.CustomerID);

You can pass the expression you want to sort in as a Lamba (this is simplified/pseudocode)

SortOrders(IEnumerable<Order> orders, Func<Order, string> func)
{
    sortedOrders = orders.OrderBy(func) 
    ....                
}


// call:
SortOrders(orders,  o => o.CustomerID);
神回复 2024-10-05 18:54:27

您可以使用 动态 LINQ 并且不需要扩展方法。只需使用 DynamicQueryable 类中的扩展并将键和方向指定为字符串即可。

var sorted = orders.OrderBy( "CustomerID desc" );

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.

var sorted = orders.OrderBy( "CustomerID desc" );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文