将 IOrderedQueryable 添加到 IQueryable

发布于 2025-01-17 17:01:31 字数 1109 浏览 0 评论 0原文

我有一种正在尝试制作的通用方法,为了实现这一点,我想将iorderedqueryable作为参数。

public async Task<IEnumerable<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> filter, IOrderedQueryable orderBy){
    IQueryable<TEntity> query = dbSet;
    query = query.Where(filter);
    query = query.OrderBy(orderBy); //This is what I want to do
    return await query.ToListAsync();
}

我想从外部传递一个iorderedqueryable,以便能够根据字符串制作功能,并能够查看我的对象的属性(没有反射),对于我的每个对象。

public static Func<IQueryable<MyObject>, string, IOrderedQueryable<Entity>> OrderBy = (query, orderBy) =>
    {
        return orderBy switch
        {
            "Id" => query.OrderBy(x => x.Id),
            "Prop1" => query.OrderBy(x => x.Prop1),
            "Prop2" => query.OrderBy(x => Prop2),
            _ => query.OrderBy(x => x.Id)
        };
    };

我不知道是否会更改订单是否会产生任何影响。

query = orderBy;
query = query.Where(filter);

或者我不知道我是否可以做concat,还是会让我失去我的条款

query = query.Concat(orderBy);

I have this generic method that I'm trying to make, and in order to achieve that I want to pass an IOrderedQueryable as a parameter.

public async Task<IEnumerable<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> filter, IOrderedQueryable orderBy){
    IQueryable<TEntity> query = dbSet;
    query = query.Where(filter);
    query = query.OrderBy(orderBy); //This is what I want to do
    return await query.ToListAsync();
}

I want to pass an IOrderedQueryable from outside in order to be able to make the function depending on a string and be able to look at the properties of my object (without reflection), for one and each of my objects.

public static Func<IQueryable<MyObject>, string, IOrderedQueryable<Entity>> OrderBy = (query, orderBy) =>
    {
        return orderBy switch
        {
            "Id" => query.OrderBy(x => x.Id),
            "Prop1" => query.OrderBy(x => x.Prop1),
            "Prop2" => query.OrderBy(x => Prop2),
            _ => query.OrderBy(x => x.Id)
        };
    };

I don't know if I change the order if it would have any impact.

query = orderBy;
query = query.Where(filter);

Or I don't know if I can do a Concat, or if that would make me lose my where clause

query = query.Concat(orderBy);

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文