Demo entry 6663295

mc_2

   

Submitted by mc_2 on Nov 29, 2017 at 21:28
Language: C#. Code size: 1.0 kB.

public abstract class GenericRepository<T> : IDisposable, IGenericRepository<T> where T : class
{
    protected readonly DbContext _context;

    protected GenericRepository(DbContext context)
    {
        _context = context;
    }

    public virtual IEnumerable<T> FindBy<TKey>(Expression<Func<T, bool>> filterPredicate,
                                                       Expression<Func<T, TKey>> orderByPredicate = null,
                                                       OrderByDirection orderByDirection = OrderByDirection.Ascending)
    {
        var baseQuery = _context.Set<T>().Where(filterPredicate);
        if (orderByPredicate != null)
        {
            return orderByDirection == OrderByDirection.Ascending
                ? baseQuery.OrderBy(orderByPredicate).ToList()
                : baseQuery.OrderByDescending(orderByPredicate).ToList();
        }
        return baseQuery.ToList();
    }
    
    public void Dispose()
    {
        _context.Dispose();
    }
    
    ///...
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).