OrderBy 的列表表达式

发布于 2024-08-03 22:20:31 字数 621 浏览 1 评论 0 原文

我希望能够存储一个表达式列表,以便稍后使用 IQueryable.OrderBy 执行,例如:

List<Expression<Func<MyType, object>>> list = new List<Expression<Func<MyType, object>>>();
  list.Add(x => x.Date);
  list.Add(x => x.ID);
  IOrderedQueryable<MyType> qry = query.OrderBy(list[0]).ThenBy(list[1]);

但是这样做会引发 InvalidOperationException - 无法按类型“System.Object”排序,因为表达式是使用 object 和 定义的不是特定类型,例如 Expression>Expression>

如何存储表达式列表,以便我可以稍后在 OrderBy 方法中执行它们吗?

I would like to be able to store a list of expressions to execute with IQueryable.OrderBy at a later time, something like:

List<Expression<Func<MyType, object>>> list = new List<Expression<Func<MyType, object>>>();
  list.Add(x => x.Date);
  list.Add(x => x.ID);
  IOrderedQueryable<MyType> qry = query.OrderBy(list[0]).ThenBy(list[1]);

However doing this throws an InvalidOperationException - Cannot order by type 'System.Object' because the expression is defined with object and not a specific type like Expression<Func<MyType, DateTime>> or Expression<Func<MyType, int>>

How can I store a list of expressions so that I can execute them later in a OrderBy method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

萌化 2024-08-10 22:20:31

您只需添加到此订单中吗?您稍后不需要动态删除内容吗?如果是这样的话,那就相对简单了:

using System;
using System.Linq;
using System.Linq.Expressions;

class QueryOrdering<T>
{
    private Func<IQueryable<T>, IOrderedQueryable<T>> function;

    public void AddOrdering<TKey>(Expression<Func<T, TKey>> keySelector)
    {
        if (function == null)
        {
            function = source => source.OrderBy(keySelector);
        }
        else
        {
            // We need to capture the current value...
            var currentFunction = function;
            function = source => currentFunction(source).ThenBy(keySelector);
        }
    }

    public IOrderedQueryable<T> Apply(IQueryable<T> source)
    {
        if (function == null)
        {
            throw new InvalidOperationException("No ordering defined");
        }
        return function(source);
    }
}

我不喜欢这个使用突变的事实......编写一个不可变的版本并不会太难,其中 AddOrdering 返回一个新的 QueryOrdering

Do you just need to add to this ordering? You don't need to dynamically remove stuff later on? If that's the case, it's relatively straightforward:

using System;
using System.Linq;
using System.Linq.Expressions;

class QueryOrdering<T>
{
    private Func<IQueryable<T>, IOrderedQueryable<T>> function;

    public void AddOrdering<TKey>(Expression<Func<T, TKey>> keySelector)
    {
        if (function == null)
        {
            function = source => source.OrderBy(keySelector);
        }
        else
        {
            // We need to capture the current value...
            var currentFunction = function;
            function = source => currentFunction(source).ThenBy(keySelector);
        }
    }

    public IOrderedQueryable<T> Apply(IQueryable<T> source)
    {
        if (function == null)
        {
            throw new InvalidOperationException("No ordering defined");
        }
        return function(source);
    }
}

I don't like the fact that this uses mutation... it wouldn't be too hard to write an immutable version, where AddOrdering returned a new QueryOrdering<T> with the new function.

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