![](/img/trans.png)
[英]is it possible to pass a IIncludableQueryable object and apply it to a DbSet
[英]pass IIncludableQueryable object to a DbSet in entity framework core
如何将IIncludableQueryable
类型的对象传递给DbSet<>
object? IQueryable
类型有效,但IIncludableQueryable
,当使用Include
和ThenInclude
时,
测试方法应该接受一个链接的IIncludableQueryable
元素( .Include().ThenInclude()
)。
测试:
public IQueryable<TEntity> Test<TEntity>(DbSet<TEntity> dbSet, Expression<Func<IIncludableQueryable<TEntity, object>>> query) where TEntity : class => dbSet.Include(query);
调用者示例:
return Test<Site>(dbSet, .ThenInclude(app => app.Client)
.ThenInclude(cl => cl.Country)
.ThenInclude(co => co.Culture)
.Include(st => st.App)
.ThenInclude(app => app.ServiceType)
.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));
经过一番阅读,我找到了解决方案。
public async Task<IQueryable<ISite>> Site(Expression<Func<Site, bool>> predicate)
{
var query = this.DbSet.Include(st => st.App)
.ThenInclude(app => app.Client)
.ThenInclude(cl => cl.Country)
.ThenInclude(co => co.Culture)
.Include(st => st.App)
.ThenInclude(app => app.ServiceType)
.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)
.Where(predicate);
return await Exec(query);
}
基地:
protected async Task<IQueryable<TImplementation>> Exec(IQueryable<TImplementation> query)
{
return await query.ToQueryableAsync();
}
ToQueryableAsync扩展:
public static async Task<IQueryable<TSource>> ToQueryableAsync<TSource>(this IQueryable<TSource> source) where TSource : IAssetsBase
{
var result = await source.ToListAsync();
return result.AsQueryable();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.