ef Core Table 关系包含计数错误
Ef Core Table relationship include count Error
我是 .NET 和 EF Core 的新手。我无法计算与 post table.
关联的 table
我想做的是显示 post 上的总点赞数。
PostManager
:
var posts = await _unitOfWork.Posts
.GetAllAsync(p => p.UserId == userId &&
p.IsActive == true,
l => l.Likes.Count());
EfBaseRepository
:
public async Task<IList<TEntity>> GetAllAsync(Expression<Func<TEntity, bool>> predicate = null, params Expression<Func<TEntity, object>>[] includeProperties)
{
IQueryable<TEntity> query = _context.Set<TEntity>();
if (predicate != null)
{
query = query.Where(predicate);
}
if (includeProperties.Any())
{
foreach (var includeProperty in includeProperties)
{
query = query.Include(includeProperty);
}
}
return await query.ToListAsync();
}
错误:
System.InvalidOperationException: The expression 'Convert(l.Likes.AsQueryable().Count(), Object)' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.
改为这样做。基本上,您想实现“延迟加载”。在您的“GetAllAsync”方法中,“includeProperties”需要 属性。在这种情况下,“喜欢”属性 所以它是“l.Likes”而不是“l.Likes.Count()”。之后您可以数数。
var posts = await _unitOfWork.Posts
.GetAllAsync(p => p.UserId == userId &&
p.IsActive == true,
l => l.Likes).ToListAsync();
var likesCount = posts.Likes.Count();
如果您是 EF Core 的新手,请不要创建通用存储库,这是无用的模式,只会增加复杂性。
正确且最快的查询是:
var likesCount = await _context.Posts
.Where(p => p.UserId == userId && p.IsActive == true)
.SelectMany(p => p.Likes)
.CountAsync();
我是 .NET 和 EF Core 的新手。我无法计算与 post table.
关联的 table我想做的是显示 post 上的总点赞数。
PostManager
:
var posts = await _unitOfWork.Posts
.GetAllAsync(p => p.UserId == userId &&
p.IsActive == true,
l => l.Likes.Count());
EfBaseRepository
:
public async Task<IList<TEntity>> GetAllAsync(Expression<Func<TEntity, bool>> predicate = null, params Expression<Func<TEntity, object>>[] includeProperties)
{
IQueryable<TEntity> query = _context.Set<TEntity>();
if (predicate != null)
{
query = query.Where(predicate);
}
if (includeProperties.Any())
{
foreach (var includeProperty in includeProperties)
{
query = query.Include(includeProperty);
}
}
return await query.ToListAsync();
}
错误:
System.InvalidOperationException: The expression 'Convert(l.Likes.AsQueryable().Count(), Object)' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.
改为这样做。基本上,您想实现“延迟加载”。在您的“GetAllAsync”方法中,“includeProperties”需要 属性。在这种情况下,“喜欢”属性 所以它是“l.Likes”而不是“l.Likes.Count()”。之后您可以数数。
var posts = await _unitOfWork.Posts
.GetAllAsync(p => p.UserId == userId &&
p.IsActive == true,
l => l.Likes).ToListAsync();
var likesCount = posts.Likes.Count();
如果您是 EF Core 的新手,请不要创建通用存储库,这是无用的模式,只会增加复杂性。
正确且最快的查询是:
var likesCount = await _context.Posts
.Where(p => p.UserId == userId && p.IsActive == true)
.SelectMany(p => p.Likes)
.CountAsync();