Skip to content

Instantly share code, notes, and snippets.

@mahmut-gundogdu
Created December 23, 2017 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahmut-gundogdu/94c1ed9f9733b725263ed53a7733ee0a to your computer and use it in GitHub Desktop.
Save mahmut-gundogdu/94c1ed9f9733b725263ed53a7733ee0a to your computer and use it in GitHub Desktop.
abp boilerplate deki QueryableExtensions.cs dosyasına orderby if eklenmiş hali. kısaca kodun işlevi verilen şart geçerli ise where kosulunu queryable ekliyor.
using System;
using System.Linq;
using System.Linq.Expressions;
using System.ServiceModel.Channels;
namespace KYGM.Framework.Core.Utilities.Helpers
{
/// <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(nameof(query));
}
return query.Skip(skipCount).Take(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;
}
public static IQueryable<TSource> OrderByIf<TSource, TKey>(this IQueryable<TSource> query, bool condition, string sortTypeName, Expression<Func<TSource, TKey>> orderByExpression)
{
return query.OrderByIf(condition, sortTypeName == "asc", orderByExpression);
}
public static IQueryable<TSource> OrderByIf<TSource, TKey>(this IQueryable<TSource> query, bool condition, bool isAsc, Expression<Func<TSource, TKey>> orderByExpression)
{
if (condition)
{
return isAsc ? query.OrderBy(orderByExpression) : query.OrderByDescending(orderByExpression);
}
return query;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment