You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何向GetAll方法的Lambda类型orderBy参数传递值?

传递GetAll方法的orderBy参数示例

你需要传入符合Func<IQueryable<T>, IOrderedQueryable<T>>类型的委托,本质是用lambda表达式指定排序逻辑,以下是几种常见场景的写法:

  • 单属性升序排序
    假设你的实体类型是Product,要按Name属性升序排列:

    _unitOfWork.GetAll(orderBy: q => q.OrderBy(p => p.Name));
    
  • 单属性降序排序
    按CreatedDate属性降序排列:

    _unitOfWork.GetAll(orderBy: q => q.OrderByDescending(p => p.CreatedDate));
    
  • 多条件组合排序
    先按CategoryId升序,再按Price降序:

    _unitOfWork.GetAll(orderBy: q => q.OrderBy(p => p.CategoryId).ThenByDescending(p => p.Price));
    
  • 结合其他参数使用
    如果同时需要传递过滤条件、关联属性,建议显式指定参数名避免顺序错误:

    _unitOfWork.GetAll(
        filter: p => p.IsActive,
        orderBy: q => q.OrderBy(p => p.Name),
        includeProperties: "Category,Supplier"
    );
    

核心逻辑就是:传入的lambda表达式要接收原始的IQueryable<T>对象,调用OrderBy/OrderByDescending/ThenBy等排序方法,返回排序后的IOrderedQueryable<T>即可。

内容的提问来源于stack exchange,提问作者Toha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 03:22:14