EF core 2.2 - 投影作为一般存储库错误的参数



>我的基本存储库中有一个这样的方法:

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);

在此阶段,我收到如下错误:

类型的表达式 'System.Collections.Generic.IAsyncEnumerable1[DAL.Models.BusinessDetails]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable1[DAL.模型.业务详情]' 方法的 'System.Collections.Generic.List1[DAL.Models.BusinessDetails] ToList[BusinessDetails](System.Collections.Generic.IEnumerable1[DAL.模型.业务详情]('

所以我有两个问题:
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();

尝试先过滤IQueryable,然后得到如下所示的投影:

q = q.Where(predicate);
if (projection != null)
{
q = q.Select(projection);
}
return await q.FirstOrDefaultAsync();

相关内容

  • 没有找到相关文章

最新更新