using
System;
using
System.Linq;
using
System.Linq.Expressions;
using
Abp.Application.Services.Dto;
namespace
Abp.Linq.Extensions
{
/// <summary>
/// Some useful extension methods for <see cref="IQueryable{T}"/>.
/// </summary>
public
static
class
QueryableExtensions
{
/// <summary>
/// Used for paging. Can be used as an alternative to Skip(...).Take(...) chaining.
/// </summary>
public
static
IQueryable<T> PageBy<T>(
this
IQueryable<T> query,
int
skipCount,
int
maxResultCount)
{
if
(query ==
null
)
{
throw
new
ArgumentNullException(
"query"
);
}
return
query.Skip(skipCount).Take(maxResultCount);
}
/// <summary>
/// Used for paging with an <see cref="IPagedResultRequest"/> object.
/// </summary>
/// <param name="query">Queryable to apply paging</param>
/// <param name="pagedResultRequest">An object implements <see cref="IPagedResultRequest"/> interface</param>
public
static
IQueryable<T> PageBy<T>(
this
IQueryable<T> query, IPagedResultRequest pagedResultRequest)
{
return
query.PageBy(pagedResultRequest.SkipCount, pagedResultRequest.MaxResultCount);
}
/// <summary>
/// Filters a <see cref="IQueryable{T}"/> by given predicate if given condition is true.
/// </summary>
/// <param name="query">Queryable to apply filtering</param>
/// <param name="condition">A boolean value</param>
/// <param name="predicate">Predicate to filter the query</param>
/// <returns>Filtered or not filtered query based on <paramref name="condition"/></returns>
public
static
IQueryable<T> WhereIf<T>(
this
IQueryable<T> query,
bool
condition, Expression<Func<T,
bool
>> predicate)
{
return
condition
? query.Where(predicate)
: query;
}
/// <summary>
/// Filters a <see cref="IQueryable{T}"/> by given predicate if given condition is true.
/// </summary>
/// <param name="query">Queryable to apply filtering</param>
/// <param name="condition">A boolean value</param>
/// <param name="predicate">Predicate to filter the query</param>
/// <returns>Filtered or not filtered query based on <paramref name="condition"/></returns>
public
static
IQueryable<T> WhereIf<T>(
this
IQueryable<T> query,
bool
condition, Expression<Func<T,
int
,
bool
>> predicate)
{
return
condition
? query.Where(predicate)
: query;
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· Apifox不支持离线,Apipost可以!
· 历时 8 年,我冲上开源榜前 8 了!
· Trae 开发工具与使用技巧
· 通过 API 将Deepseek响应流式内容输出到前端
· 上周热点回顾(3.10-3.16)