EF Core - 使用投影并包含实体的集合
EF Core - use projection and include a collection of an entity
我有一个基本存储库:
public async Task<TEntity> GetByCondition(Expression<Func<TEntity, bool>> predicate, Func<DbSet<TEntity>, IQueryable<TEntity>> baseQuery = null, Expression<Func<TEntity, TEntity>> projection = null)
{
IQueryable<TEntity> q = _context.Set<TEntity>();
if (baseQuery != null)
{
q = baseQuery(_context.Set<TEntity>());
}
q = q.Where(predicate);
if (projection != null)
{
q = q.Select(projection);
}
return await q.FirstOrDefaultAsync();
}
我尝试获取业务实体并包含 image.inverseParents 但没有成功:
Expression<Func<Businesses, bool>> predicate = x => x.Id == id;
Expression<Func<Businesses, Businesses>> projection = x => new Businesses
{
BusinessImages = x.BusinessImages
.Where(bi => bi.Status == (int)EnumGringo.LU_Status.active && bi.Image.Status == (int)EnumGringo.LU_Status.active)
.Select(bi => new BusinessImages
{
//Won't complie
Image = bi.Image.Include(i=>i.InverseParent)
}).ToList()
};
Businesses business = await _repository.GetByCondition(predicate, projection: projection);
我怎样才能做这样的事情?
Image = bi.Image.Include(i=>i.InverseParent)
我尝试过的事情:
Image = bi.Image.Select(i=>i.InverseParent.Parent).FirstOrDefault()
添加
IQueryable<Businesses> baseQuery(DbSet<Businesses> x) => x
.Include(c => c.BusinessImages)
.ThenInclude(c => c.Image)
.ThenInclude(c => c.InverseParent);
Businesses business = await _repository.GetByCondition(predicate, projection: projection);
基本存储库忽略基本查询。
投影查询忽略 Include
,一旦您使用 Select
,您需要继续使用 select。
这对我有用:
BusinessImages = x.BusinessImages
.Where(bi => bi.Status == (int)EnumGringo.LU_Status.active && bi.Image.Status == (int)EnumGringo.LU_Status.active)
.Select(bi => new BusinessImages
{
Image = new UserImages
{
InverseParent = bi.Image.InverseParent
}
})
我有一个基本存储库:
public async Task<TEntity> GetByCondition(Expression<Func<TEntity, bool>> predicate, Func<DbSet<TEntity>, IQueryable<TEntity>> baseQuery = null, Expression<Func<TEntity, TEntity>> projection = null)
{
IQueryable<TEntity> q = _context.Set<TEntity>();
if (baseQuery != null)
{
q = baseQuery(_context.Set<TEntity>());
}
q = q.Where(predicate);
if (projection != null)
{
q = q.Select(projection);
}
return await q.FirstOrDefaultAsync();
}
我尝试获取业务实体并包含 image.inverseParents 但没有成功:
Expression<Func<Businesses, bool>> predicate = x => x.Id == id;
Expression<Func<Businesses, Businesses>> projection = x => new Businesses
{
BusinessImages = x.BusinessImages
.Where(bi => bi.Status == (int)EnumGringo.LU_Status.active && bi.Image.Status == (int)EnumGringo.LU_Status.active)
.Select(bi => new BusinessImages
{
//Won't complie
Image = bi.Image.Include(i=>i.InverseParent)
}).ToList()
};
Businesses business = await _repository.GetByCondition(predicate, projection: projection);
我怎样才能做这样的事情?
Image = bi.Image.Include(i=>i.InverseParent)
我尝试过的事情:
Image = bi.Image.Select(i=>i.InverseParent.Parent).FirstOrDefault()
添加
IQueryable<Businesses> baseQuery(DbSet<Businesses> x) => x
.Include(c => c.BusinessImages)
.ThenInclude(c => c.Image)
.ThenInclude(c => c.InverseParent);
Businesses business = await _repository.GetByCondition(predicate, projection: projection);
基本存储库忽略基本查询。
投影查询忽略 Include
,一旦您使用 Select
,您需要继续使用 select。
这对我有用:
BusinessImages = x.BusinessImages
.Where(bi => bi.Status == (int)EnumGringo.LU_Status.active && bi.Image.Status == (int)EnumGringo.LU_Status.active)
.Select(bi => new BusinessImages
{
Image = new UserImages
{
InverseParent = bi.Image.InverseParent
}
})