SQLSugar 多表统计数量、平均值

 public Dictionary<string, string> GetScoreInfo(int classId)
        {   
            Dictionary<string, string> dic = new Dictionary<string, string>();

            //获取参加考试学生总人数
          string  count=  Db.Queryable<Students, ScoreList>((stu, score) => new object[] {
            JoinType.Inner,stu.StudentId==score.StudentId
            }).Where((stu,score)=> stu.ClassId == classId).Count().ToString();
            //获取CSharp科目平均值
            string avgCSharop = Db.Queryable<Students, ScoreList>((stu, score) => new object[] {
            JoinType.Inner,stu.StudentId==score.StudentId
            }).Where((stu,score)=> stu.ClassId == classId). Avg((stu,score) => score.CSharp).ToString();
      /*

      string avgCSharop = Db.Queryable<ScoreList>().Where(score => SqlFunc.Subqueryable<Students>().Where(
      stu => stu.StudentId == score.StudentId && stu.ClassId == classId
        ).Any()).Avg(score => score.CSharp).ToString();

      string avgSQL = objectDb.AsQueryable().Where(score => SqlFunc.Subqueryable<Students>().Where(
      stu => stu.StudentId == score.StudentId && stu.ClassId == classId).Any()).Avg(s => s.SQLServerDB).ToString();

        */

//获取SQLServer科目平均值
            string  avgSQL = Db.Queryable<Students, ScoreList>((stu, score) => new object[] {
            JoinType.Inner,stu.StudentId==score.StudentId
            }).Where((stu,score)=> stu.ClassId == classId).Avg((stu, score) => score.SQLServerDB).ToString();
            //获取缺考学生总人数
            string absentCount = Db.Queryable<Students>().Where(stu => SqlFunc.Subqueryable<ScoreList>()
             .Where(score =>  stu.StudentId == score.StudentId) .NotAny()).Where(stu=>stu.ClassId==classId) .Count().ToString();

            dic.Add("count", count);
            dic.Add("avgCsharp", avgCSharop);
            dic.Add("avgSQL", avgSQL);
            dic.Add("absentCount", absentCount);
            return dic;
        }

 

posted @ 2021-10-25 15:17  ssh001  阅读(1165)  评论(0)    收藏  举报