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