Aslan Akbey
How to structure query functions in services
public async Task<IQueryable<T>> GetListAsync(Expression<Func<T, bool>>? predicate = null, Func<IQueryable<T>, IOrderedQueryable<T>>? orderBy = null, Func<IQueryable<T>, IIncludableQueryable<T, object>>? include = null, bool enableTracking = true, CancellationToken cancellationToken = default)
{
IQueryable<T> queryable = Table.AsQueryable();
if (!enableTracking) queryable = queryable.AsNoTracking();
if (include != null) queryable = include(queryable);
if (predicate != null) queryable = queryable.Where(predicate);
if (orderBy != null)
return await Task.FromResult(orderBy(queryable));
return await Task.FromResult(queryable);
}
22 replies
How to structure query functions in services
you can use IIncludableQueryable
https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.query.iincludablequeryable-2?view=efcore-8.0
22 replies