1. 程式人生 > 其它 >SQLSugar 多表統計數量、平均值

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(); //獲取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; }