繁体   English   中英

使用 linq 在 EF Core 中计算评分平均值

[英]Using linq to calculate rating average in EF Core

用户可以按城市、品牌、服务类型和评级搜索公司。 我开发了这样的查询,但在评级部分出现错误。 错误信息如下:

LINQ 表达式 '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 })' 无法翻译。 以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估。 有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038

添加评分部分后我遇到了这个错误。 我怎样才能解决这个问题? 我认为以这种方式提取数据不会有效。 我应该如何开发这个地方? 我还与您分享了我用于查询的代码。

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 进行分组,例如 Companies.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 中的分组有局限性 - 您只能返回分组键和聚合结果。 要返回整个记录,您必须加入原始查询增益。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM