EF 核心 2.2 - 投影作为一般存储库错误的参数
EF core 2.2 - projection as a parameter to a generic respository error
我的基本存储库中有一个这样的方法:
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>());
}
if (projection != null)
{
q = q.Select(projection);
}
return await q.Where(predicate).FirstOrDefaultAsync();
}
我试着这样调用这个方法:
Expression<Func<Businesses, bool>> predicate = x => x.Id == id && x.Status == (int)EnumGringo.LU_Status.active;
IQueryable<Businesses> baseQuery(DbSet<Businesses> x) => x.Include(c => c.BusinessDetails)
.Include(c => c.BusinessFacilities).ThenInclude(c => c.Facility)
.Include(c => c.BusinessImages);
Expression<Func<Businesses, Businesses>> projection = x => new Businesses
{
BusinessDetails = x.BusinessDetails.Where(bd => bd.Status == (int)EnumGringo.LU_Status.active && bd.LanguageTypeId == (int)EnumGringo.Lu_LanguageTypes.he).ToList(),
Name = "Name projection"
};
Businesses business = await _repository.GetByCondition(predicate, baseQuery, projection);
在这个阶段,我收到如下错误:
Expression of type
'System.Collections.Generic.IAsyncEnumerable1[DAL.Models.BusinessDetails]'
cannot be used for parameter of type
'System.Collections.Generic.IEnumerable
1[DAL.Models.BusinessDetails]'
of method
'System.Collections.Generic.List1[DAL.Models.BusinessDetails]
ToList[BusinessDetails](System.Collections.Generic.IEnumerable
1[DAL.Models.BusinessDetails])'
所以我有两个问题:
1) 我如何让它工作?
2) 如果我发送投影参数或稍后像这样过滤我的实体,有什么不同吗?
Businesses business = await _repository.GetByCondition(predicate, baseQuery);
business.BusinessDetails = business.BusinessDetails.Where(bd => bd.Status == (int)EnumGringo.LU_Status.active && bd.LanguageTypeId == (int)EnumGringo.Lu_LanguageTypes.he).ToList();
问题在这里:
if (projection != null)
{
q = q.Select(projection);
}
return await q.Where(predicate).FirstOrDefaultAsync();
尝试先过滤 IQuerable
然后得到这样的投影:
q = q.Where(predicate);
if (projection != null)
{
q = q.Select(projection);
}
return await q.FirstOrDefaultAsync();
我的基本存储库中有一个这样的方法:
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>());
}
if (projection != null)
{
q = q.Select(projection);
}
return await q.Where(predicate).FirstOrDefaultAsync();
}
我试着这样调用这个方法:
Expression<Func<Businesses, bool>> predicate = x => x.Id == id && x.Status == (int)EnumGringo.LU_Status.active;
IQueryable<Businesses> baseQuery(DbSet<Businesses> x) => x.Include(c => c.BusinessDetails)
.Include(c => c.BusinessFacilities).ThenInclude(c => c.Facility)
.Include(c => c.BusinessImages);
Expression<Func<Businesses, Businesses>> projection = x => new Businesses
{
BusinessDetails = x.BusinessDetails.Where(bd => bd.Status == (int)EnumGringo.LU_Status.active && bd.LanguageTypeId == (int)EnumGringo.Lu_LanguageTypes.he).ToList(),
Name = "Name projection"
};
Businesses business = await _repository.GetByCondition(predicate, baseQuery, projection);
在这个阶段,我收到如下错误:
Expression of type 'System.Collections.Generic.IAsyncEnumerable
1[DAL.Models.BusinessDetails]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable
1[DAL.Models.BusinessDetails]' of method 'System.Collections.Generic.List1[DAL.Models.BusinessDetails] ToList[BusinessDetails](System.Collections.Generic.IEnumerable
1[DAL.Models.BusinessDetails])'
所以我有两个问题:
1) 我如何让它工作?
2) 如果我发送投影参数或稍后像这样过滤我的实体,有什么不同吗?
Businesses business = await _repository.GetByCondition(predicate, baseQuery);
business.BusinessDetails = business.BusinessDetails.Where(bd => bd.Status == (int)EnumGringo.LU_Status.active && bd.LanguageTypeId == (int)EnumGringo.Lu_LanguageTypes.he).ToList();
问题在这里:
if (projection != null)
{
q = q.Select(projection);
}
return await q.Where(predicate).FirstOrDefaultAsync();
尝试先过滤 IQuerable
然后得到这样的投影:
q = q.Where(predicate);
if (projection != null)
{
q = q.Select(projection);
}
return await q.FirstOrDefaultAsync();