如何在实体框架中获得平均评级

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在实体框架中获得平均评级相关的知识,希望对你有一定的参考价值。

嗨,我的代码如下

var query = (from R in db.Registrations
            join c in db.Campus
            on R.CampusId equals c.CampusId
            from tsr in db.TutorStudentRequests.Where(t => t.TutorId == R.RegistrationId).DefaultIfEmpty()
            where R.UserTypeId == 2 && tsr.StatusId!=3
            orderby R.Name ascending
            select new
            {
                RegistrationId = R.RegistrationId,
                Name = R.Name,
                Email = R.Email,
                Phone = R.Phone,
                Password = c.CampusName,
                IsGPA = R.IsGPA,
                IsActive = R.IsActive,
                StripeId = R.StripeId,
                CreatedOn = R.CreatedOn,
                UserTypeId =tsr.StudentReviewRating
            })
        .ToList();

我有注册表有单行,我还有另一个表TutorStudentRequests有多行。如何在TutorStudentRequests表中获得列名StudentReviewRating的平均值?

结构可能看起来像:

登记表

RegistrationId   Name   Email 
   1             abc    abc@gmail.com
   2             xyz    xyz@gmail.com

TutorStudentRequests表

Id  TutorId(RegistrationId of F.k.)   StudentReviewRating
 1    1                                    5
 2    1                                    2
 3    1                                    1
 4    2                                    3

我希望UserTypeId数据作为每个TutorId的StudentReviewRating的平均值

试着

var query = (from R in db.Registrations
        join c in db.Campus
        on R.CampusId equals c.CampusId
        from tsr in db.TutorStudentRequests.Where(t => t.TutorId == R.RegistrationId).DefaultIfEmpty()
        where R.UserTypeId == 2 && tsr.StatusId != 3
        group R by new
        {
            R.RegistrationId,
            R.Name,
            R.Email,
            R.Phone,
            c.CampusName,
            R.IsGPA,
            R.IsActive,
            R.StripeId,
            R.CreatedOn,                      
        } into groupings
        select new
        {
            RegistrationId = groupings.Key.RegistrationId,
            Name = groupings.Key.Name,
            Email = groupings.Key.Email,
            Phone = groupings.Key.Phone,
            Password = groupings.Key.CampusName,
            IsGPA = groupings.Key.IsGPA,
            IsActive = groupings.Key.IsActive,
            StripeId = groupings.Key.StripeId,
            CreatedOn = groupings.Key.CreatedOn,
            Average = groupings.Average(p=>Convert.ToDecimal(p.StudentReviewRating))
        });

但它表示注册不包含'StudentReviewRating'的定义。

怎么了?

答案

如何通过导师分组,计算平均值作为总评级/评级数

var query = from request in data
            group request by request.TutorId into groupings
            let total = groupings.Sum(p=>p.StudentReviewRating)
            let number = groupings.Count()
            let average = (decimal)total/number
            select new 
            {
                TutorId = groupings.Key,
                Summary = new 
                {
                    Total = total,
                    Number = number,
                    Average = average
                }
            }

对于给定的测试用例,结果看起来像这样

TutorId Summary
1   Total   8
    Number  3
    Average 2.66
2   Total   3
    Number  1
    Average 3

编辑额外的连接和扩展的组

var query = from registration in registrations
            join request in requests 
                on registration.RegistrationId equals request.TutorId
            group request by new 
            {
                registration.RegistrationId,
                registration.Name, 
                request.TutorId,
            } into groupings
            select new 
            {
                RegistrationId = groupings.Key.RegistrationId,
                TutorId = groupings.Key.TutorId,
                Average = groupings.Average(p=>p.StudentReviewRating)
            }
另一答案

嗨,你可以用这个来拥有你想要的数学https://numerics.mathdotnet.com/

以上是关于如何在实体框架中获得平均评级的主要内容,如果未能解决你的问题,请参考以下文章

如何让 mysql 与标签搜索一起获得平均评分?

Sequelize js如何获取关联模型的平均值(聚合)

使用实体框架迁移时 SQL Server 连接抛出异常 - 添加代码片段

在 4 个单独的图上绘制每个评级的组平均值

没有分组的实体框架总和和平均值

如何优化我的实体框架查询以获得更好的性能和更高的可读性