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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM