简体   繁体   中英

combine two IIncludedQueryable on same DbSet in Entity Framework Core

I'm looking to add an IIncludedQueryable object to an existing IIncludedQueryable object.

Some of my objects inherit IClientAccess and I want to include the related entity's without having to change IQueryable code for each entity.

My code :

public static IQueryable<IStore> StoreLite(IQueryable<IStore> dbSet)
{
    var result = dbSet
        .Include(str => str.VATs)
            .ThenInclude(vat => vat.VAT)
                .ThenInclude(vat => vat.Culture)
                    .ThenInclude(cult => cult.Items)
                        .ThenInclude(itm => itm.Culture)
        .Include(str => str.Options)
            .ThenInclude(opt => opt.Items)
                .ThenInclude(itm => itm.Option)
        .Include(str => str.Cultures)
            .ThenInclude(cult => cult.Items)
                .ThenInclude(itm => itm.Culture)
                    .ThenInclude(cult => cult.Items)
                        .ThenInclude(itm => itm.Culture)
        .Include(str => str.Pages)
            .ThenInclude(page => page.Sections)
                .ThenInclude(section => section.Elements);

    return result;
}

public static IQueryable<IClientAccess> ClientAccess(IQueryable<IClientAccess> dbSet)
{
    var result = dbSet
        .Include(clnt => clnt.ClientAccess)
            .ThenInclude(acc => acc.Items)
                .ThenInclude(itm => itm.Client);

    return result;
}

So what I'm thinking in StoreLite :

var result = dbSet...(existing);

result += ClientAccess(dbSet); //I know this don't work

My solution is to pass the IQueryable from ClientAccess to the StoreLite method in ShopExpressions

GenericExpressions.cs :

public static IQueryable<T> ClientAccess<T>(IQueryable<T> dbSet) where T : class, IBaseData, IClientAccess
{
    var result = dbSet
        .Include(clnt => clnt.ClientAccess)
            .ThenInclude(acc => acc.Items)
                .ThenInclude(itm => itm.Client);

    return result;
}

ShopExpressions.cs :

public static IQueryable<IStore> StoreLite(IQueryable<IStore> dbSet)
{
    var result = dbSet
        .Include(str => str.VATs)
            .ThenInclude(vat => vat.VAT)
                .ThenInclude(vat => vat.Culture)
                    .ThenInclude(cult => cult.Items)
                        .ThenInclude(itm => itm.Culture)
        .Include(str => str.Options)
            .ThenInclude(opt => opt.Items)
                .ThenInclude(itm => itm.Option)
        .Include(str => str.Cultures)
            .ThenInclude(cult => cult.Items)
                .ThenInclude(itm => itm.Culture)
                    .ThenInclude(cult => cult.Items)
                        .ThenInclude(itm => itm.Culture)
        .Include(str => str.Pages)
            .ThenInclude(page => page.Sections)
                .ThenInclude(section => section.Elements);

    return result;
}

In use :

public async Task<IStore> GetStoreLite(int id)
{
    IStore result = await ShopExpressions.StoreLite(GenericExpressions.ClientAccess(this.Worker.GetRepo<Store>().DbSet))
        .SingleAsync(str => str.Id.Equals(id));

    this.Worker.ValidateClientAccess(result);

    return 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