namespace Indotalent.ConfigBackEnd.Extensions; using Indotalent.Data.Interfaces; using Indotalent.Shared.Models; using Microsoft.EntityFrameworkCore; using System.Linq.Expressions; public static class IQueryableExtensions { public static IQueryable WhereIf( this IQueryable query, bool condition, Expression> predicate) { return condition ? query.Where(predicate) : query; } public static async Task> ToPagedListAsync( this IQueryable query, int skip, int top) { top = top <= 0 ? 5 : top; skip = skip < 0 ? 0 : skip; var count = await query.CountAsync(); var items = await query .Skip(skip) .Take(top) .ToListAsync(); return new PagedList(items, count, skip, top); } public static IQueryable OrderByPropertyName( this IQueryable query, string? propertyName, bool isDescending) { if (string.IsNullOrWhiteSpace(propertyName)) return query; var parameter = Expression.Parameter(typeof(T), "x"); var property = Expression.Property(parameter, propertyName); var lambda = Expression.Lambda(property, parameter); var methodName = isDescending ? "OrderByDescending" : "OrderBy"; var resultExpression = Expression.Call( typeof(Queryable), methodName, new Type[] { typeof(T), property.Type }, query.Expression, Expression.Quote(lambda)); return query.Provider.CreateQuery(resultExpression); } public static IQueryable NotDeletedOnly(this IQueryable query) where T : class, IHasIsDeleted { return query.Where(x => !x.IsDeleted); } }