IQueryable 扩展
PageBy
public static IQueryable<T> PageBy<T>(this IQueryable<T> query, int skipCount, int maxResultCount)
{
Check.NotNull(query, "query");
return query.Skip(skipCount).Take(maxResultCount);
}
WhereIf
public static TQueryable WhereIf<T, TQueryable>(this TQueryable query, bool condition, Expression<Func<T, bool>> predicate) where TQueryable : IQueryable<T>
{
Check.NotNull(query, "query");
if (!condition)
{
return query;
}
return (TQueryable)query.Where(predicate);
}
OrderByIf
public static TQueryable OrderByIf<T, TQueryable>(this TQueryable query, bool condition, string sorting) where TQueryable : IQueryable<T>
{
Check.NotNull(query, "query");
if (!condition)
{
return query;
}
return (TQueryable)query.OrderBy(sorting);
}
IncludeIf
导航属性
public static IQueryable<T> IncludeIf<T, TProperty>(this IQueryable<T> source, bool condition, Expression<Func<T, TProperty>> path) where T : class
{
if (!condition)
{
return source;
}
return source.Include(path);
}
示例
.IncludeIf(input.IncludeCuttingPlan, u => u.CuttingPlans.OrderBy(p => p.StreamNo).ThenBy(p => p.Rank));
集合扩展
对ICollection的扩展
//集合中,按条件删除
HeatPlans.RemoveAll(u => u.CastNo == castNo);