使用 linq 计算 EF Core 中的评分平均值
Using linq to calculate rating average in EF Core
用户可以按城市、品牌、服务类型和评级搜索公司。我开发了这样的查询,但在评级部分出现错误。报错信息如下:
The LINQ expression 'DbSet
.Join(
outer: DbSet,
inner: f => f.Id,
outerKeySelector: c => c.FirmId,
innerKeySelector: (f, c) => new TransparentIdentifier<Firm, Comment>(
outer = f,
inner = c
))
.GroupBy(
source: ti => ti.Outer,
keySelector: ti => new {
firm = ti.Outer,
rating = ti.Inner.Rate
})' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly
by inserting a call to either AsEnumerable(), AsAsyncEnumerable(),
ToList(), or ToListAsync(). See
https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
我在添加评分部分后遇到了这个错误。我怎样才能解决这个问题?我认为通过这种方式拉取数据不会有效。我应该如何开发这个地方?我还与您分享我用于查询的代码。
public async Task<IEnumerable<Firm>> GetFirmsForCustomerSearch(int cityId, int brandId, int serviceTypeId, int rate)
{
var query = from firms in AracsalContext.Firm select firms;
if (brandId > 0)
query = from firms in query
join firmBrands in AracsalContext.Firmbrand on new { f1 = firms.Id, f2 = brandId } equals new { f1 = firmBrands.FirmId, f2 = firmBrands.BrandId }
select firms;
if (serviceTypeId > 0)
query = from firms in query
join firmServices in AracsalContext.Firmservice on new { f1 = firms.Id, f2 = serviceTypeId } equals new { f1 = firmServices.FirmId, f2 = firmServices.ServiceId }
select firms;
if (cityId > 0)
query = from firms in query
where firms.CityId == cityId
select firms;
if (rate > 0)
{
query = from firms in query
join comments in AracsalContext.Comment on firms.Id equals comments.FirmId
group new
{
firm = firms,
rating = comments.Rate
} by firms into g
where g.Average(r => r.rating) > rate
select g.Key;
}
var result = await query.ToListAsync();
return result;
}
非常感谢。
斋月
您需要按一些 ID 分组,例如 firms.Id
query = from firms in query
join comments in AracsalContext.Comment on firms.Id equals comments.FirmId
group new
{
firm = firms,
rating = comments.Rate
} by firms.Id into g
where g.Average(r => r.rating) > rate
select g.Key;
SQL 中的分组有限制 - 您可以 return 仅分组键和聚合结果。对于 return 整条记录,您必须加入原始查询增益。
if (rate > 0)
{
filteredByRate =
from firms in query
join comments in AracsalContext.Comment on firms.Id equals comments.FirmId
group new
{
rating = comments.Rate
} by new { firms.Id } into g
where g.Average(r => r.rating) > rate
select g.Key;
query =
from films in query
join f in filteredByRate on films.Id equals f.Id
select films;
}
用户可以按城市、品牌、服务类型和评级搜索公司。我开发了这样的查询,但在评级部分出现错误。报错信息如下:
The LINQ expression 'DbSet .Join( outer: DbSet, inner: f => f.Id, outerKeySelector: c => c.FirmId, innerKeySelector: (f, c) => new TransparentIdentifier<Firm, Comment>( outer = f, inner = c )) .GroupBy( source: ti => ti.Outer, keySelector: ti => new { firm = ti.Outer, rating = ti.Inner.Rate })' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
我在添加评分部分后遇到了这个错误。我怎样才能解决这个问题?我认为通过这种方式拉取数据不会有效。我应该如何开发这个地方?我还与您分享我用于查询的代码。
public async Task<IEnumerable<Firm>> GetFirmsForCustomerSearch(int cityId, int brandId, int serviceTypeId, int rate)
{
var query = from firms in AracsalContext.Firm select firms;
if (brandId > 0)
query = from firms in query
join firmBrands in AracsalContext.Firmbrand on new { f1 = firms.Id, f2 = brandId } equals new { f1 = firmBrands.FirmId, f2 = firmBrands.BrandId }
select firms;
if (serviceTypeId > 0)
query = from firms in query
join firmServices in AracsalContext.Firmservice on new { f1 = firms.Id, f2 = serviceTypeId } equals new { f1 = firmServices.FirmId, f2 = firmServices.ServiceId }
select firms;
if (cityId > 0)
query = from firms in query
where firms.CityId == cityId
select firms;
if (rate > 0)
{
query = from firms in query
join comments in AracsalContext.Comment on firms.Id equals comments.FirmId
group new
{
firm = firms,
rating = comments.Rate
} by firms into g
where g.Average(r => r.rating) > rate
select g.Key;
}
var result = await query.ToListAsync();
return result;
}
非常感谢。 斋月
您需要按一些 ID 分组,例如 firms.Id
query = from firms in query
join comments in AracsalContext.Comment on firms.Id equals comments.FirmId
group new
{
firm = firms,
rating = comments.Rate
} by firms.Id into g
where g.Average(r => r.rating) > rate
select g.Key;
SQL 中的分组有限制 - 您可以 return 仅分组键和聚合结果。对于 return 整条记录,您必须加入原始查询增益。
if (rate > 0)
{
filteredByRate =
from firms in query
join comments in AracsalContext.Comment on firms.Id equals comments.FirmId
group new
{
rating = comments.Rate
} by new { firms.Id } into g
where g.Average(r => r.rating) > rate
select g.Key;
query =
from films in query
join f in filteredByRate on films.Id equals f.Id
select films;
}