ASP.Net Core MVC: Join two tables in Generic Repository class

AliYilmaz
 
on Jun 07, 2022 04:35 AM
3221 Views

Hi,

I am using generic repository in my project. But in a situation I need to join two tables. In this case, I need to include.

How can I add repository me include in this way.

IReadRepository

public interface IReadRepository<T> : IRepository<T> where T : BaseEntity
{
    IQueryable<T> GetAll();
 
    IQueryable<T> GetWhere(Expression<Func<T, bool>> method);
 
    Task<T> GetSingleAsync(Expression<Func<T, bool>> method);
 
    Task<T> GetByIdAsync(string id);
}

Read Repository

public class ReadRepository<T> : IReadRepository<T> where T : BaseEntity
{
    private readonly CustomsDbContext _context;
 
    public ReadRepository(CustomsDbContext context)
    {
        _context = context;
    }
 
    public DbSet<T> Table => _context.Set<T>();
 
    public IQueryable<T> GetAll()
    {
        return Table;
    }
 
    public IQueryable<T> GetWhere(Expression<Func<T, bool>> method)
    {
        return Table.Where(method);
    }
 
    public async Task<T> GetSingleAsync(Expression<Func<T, bool>> method)
    {
        return await Table.FirstOrDefaultAsync(method);
    }
 
    public async Task<T> GetByIdAsync(string id)
    {
        //return await Table.FirstOrDefaultAsync(data => data.Id == Guid.Parse(id));
        //Orm FindAsync desteklemiyorsa Marker Patter uygulayabilirsin.
 
        return await Table.FindAsync(Guid.Parse(id));
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download