【问题标题】:Filtering on Linq that contains Custom Type using ODataQueryOptions使用 ODataQueryOptions 过滤包含自定义类型的 Linq
【发布时间】:2017-01-05 01:52:09
【问题描述】:

在执行服务器端操作的 MVC 中实现 Kendo Grid 时,我发现自己处于一个棘手的境地,即我必须使用 linq 过滤、排序、分页我的数据。我得到的所有信息都是 ODataQueryOptions 类型的。

(不确定是否需要提及,但为了完整起见,我们通过UnitOfWork模式进行查询操作)

所以执行查询的操作复制如下:

public static List<T> GetT(this IRepositoryAsync<T> repository, ODataQueryOptions<T> options)
    {
        var query = repository.Query().Tracking(false).Include(x => x.T2)
            .Select(s => new
            {
                P1 = s.P1,
                P2 = s.P2,
                P3 = s.T2.P1 + "," + s.T2.P2
            })
            .Select(s => new T1
            {
                P1 = s.P1,
                P2 = s.P2,
                P3 = s.P3
            });

        if (options.Skip != null)
            query = query.Skip(options.Skip.Value);
        if (options.Top != null)
            query = query.Take(options.Top.Value);

        return query.ToList();
    }

现在,我想知道是否有选项将剩余的 oData 选项应用于 linq 查询(如您所见,$skip 和 $top 已应用,我们如何应用 OrderByQueryOption 和 FilterQueryOption)。

【问题讨论】:

    标签: c# linq asp.net-web-api odata unit-of-work


    【解决方案1】:

    OrderByQueryOption 中有一个属性OrderByNodes,您可以使用它来构造您的查询,但在FilterQueryOption 中,很难翻译FilterClause,您需要FilterBinder 为您执行此操作,但它在5.9.1,在6.0.0公开。

    【讨论】:

    • 我直接从 OrderByQueryOption 获得 Func, IOrderedQueryable> 类型的东西会帮助我(因为在 UnitOfWork 中执行 Order by 的方法期望这种类型)
    猜你喜欢
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    相关资源
    最近更新 更多