简体   繁体   中英

is it possible to pass a IIncludableQueryable object and apply it to a DbSet

I would like to separate the query part from the View function, and pass it as an object.

Current :

public async Task<IEnumerable<TType>> View<TType, TImplementation>(Expression<Func<TImplementation, bool>> predicate)   where TType : IView
                                                                                                                        where TImplementation : View
{
    var result = await this.DBContext.GetWorker<TImplementation>().DbSet
        .Include(vw => vw.Site)
            .ThenInclude(st => st.App)
                .ThenInclude(ap => ap.Client)
        .Include(vw => vw.Site)
            .ThenInclude(st => st.CORSEntries)
        .Include(vw => vw.Site)
            .ThenInclude(st => st.MetaEntries)
        .Where(predicate).ToListAsync();

    return (IEnumerable<TType>)result;
}

What I'm looking for :

public async Task<TImplementation> ViewSingle<TImplementation>(Expression<Func<TImplementation, bool>> predicate, IIncludableQueryable<TImplementation> query) where TImplementation : View
{
    var result = await this.DBContext.GetWorker<TImplementation>().DbSet
        ?? query ??
        .SingleAsync(predicate);

    return result;
}

what would the syntax be?

So I ended up moving the part where I get the DbSet to a function in the base class instead, and it basically does the same job and reduced the need to write the same code multiple times.

Base :

public abstract class ExpressionBase
{
    private readonly IAssetsDBContextAccessor _db;

    public ExpressionBase(IAssetsDBContextAccessor db)
    {
        _db = db;
    }

    internal IQueryable<TType> Exec<TType, TIplementation>(Expression<Func<TIplementation, bool>> predicate)    where TType : IAssetsBase
                                                                                                                where TIplementation : AssetsBase
    {
        var result = this.DBContext.GetWorker<TIplementation>().DbSet
            .Where(predicate);

        return (IQueryable<TType>)result;
    }

    internal IAssetsDBContextAccessor DBContext => _db;
}

Inhereting ExpressionBase :

public class SitesExpressions : ExpressionBase
{
    public SitesExpressionsBeta(IAssetsDBContextAccessor db) : base(db) { }

    public async Task<IQueryable<ISite>> Site(Expression<Func<Site, bool>> predicate)
    {
        var result = await this.Exec<ISite, Site>(predicate)
            .Include(st => st.App)
                .ThenInclude(app => app.Client)
            .Include(st => st.CORSEntries)
            .Include(st => st.DataConnection)
            .Include(st => st.Features)
                .ThenInclude(ft => ft.Cultures)
                    .ThenInclude(clt => clt.Culture)
            .Include(st => st.MetaEntries)
                .ThenInclude(mt => mt.Culture)
            .Include(st => st.Views)
                .ThenInclude(vw => vw.MetaEntries)
                    .ThenInclude(mt => mt.Culture)
            .ToListAsync();

        return (IQueryable<ISite>)result;
    }

    public async Task<IQueryable<IView>> View(Expression<Func<View, bool>> predicate)
    {
        var result = await this.Exec<IView, View>(predicate)
            .Include(vw => vw.MetaEntries)
                .ThenInclude(mt => mt.Culture)
            .ToListAsync();

        return (IQueryable<IView>)result;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM