简体   繁体   English

如何在 LINQ 中执行以下查询?

[英]How to perform the following queries in LINQ?

I want to perform the following queries with LINQ:我想用 LINQ 执行以下查询:

  • count of all units所有单位的计数
  • sum of all units scored marks and Totalmarks所有单元得分和总分的总和
  • average: Scoredmarks%Totalmarks*100 4) Rank平均值:Scoremarks%Totalmarks*100 4) 排名
  • Rank Based on score (if user1 completes 2 units with unit id 100 and 101 only that should compare with other users) Rank 基于分数(如果用户 1 完成了 2 个单元,单元 id 为 100 和 101,则应与其他用户进行比较)

DB: D B:

RID   UserID  CourseID    SemID   SubjectID   UnitID  ScoredMarks TotalMarks  No_Attempts               CreatedDate ModifiedDate
  1     1021       109     3000        2006      100           30       100             1   2019-02-12 00:00:00.000 NULL
  2     1021       109     3000        2006      101           40       100             1   2019-02-18 00:00:00.000 NULL
  3     1021       109     3000        2006      102           85       100             1   2019-02-19 00:00:00.000 NULL
  4     1022       109     3000        2006      101           80       100             1   2019-02-19 00:00:00.000 NULL
  5     1022       109     3000        2006      100           75       100             1   2019-02-19 00:00:00.000 NULL

Code:代码:

public CalculatePerform(int? Student_ID, int? CourseID, int? SemID, int? SubjectID)
{
   var ScoreCard = from i in dbcontext.Stu_Result
                   where i.UserID == Student_ID && 
                         i.CourseID == CourseID && 
                         i.SemID == SemID && 
                         i.SubjectID == SubjectID
}

Your adapted function along with an extra class I introduced to store the result:您改编的函数以及我引入的用于存储结果的额外类:

public class UserRankObject
{
    public int Rank { get; set; }
    public int UserId { get; set; }
    public float AverageScore { get; set; }
}
public UserRankObject GetUserRankObject(int? Student_ID, int? CourseID, int? SemID, int? SubjectID)
{
    // TODO: add null checks for arguments, or make them non-nullable.
    var scoreCard =
        dbcontext.Stu_Result
            .Where(u => u.CourseID == CourseID
                     && u.SemID == SemID
                     && u.SubjectID == SubjectID);

    return scoreCard.GroupBy(u => u.UserID)
       .OrderByDescending(g => g.Average(u => u.ScoredMarks / u.TotalMarks * 100))
       .Select((g, i) => new UserRankObject
       {
           UserId = g.Key,
           Rank = i + 1,
           AverageScore = g.Average(u => u.ScoredMarks / u.TotalMarks * 100)
       })
       .Single(u => u.UserId == Student_ID);
}

Usage:用法:

var ranked = new List<UserRankObject>()
{
    GetUserRankObject(1022, 109, 3000, 2006),
    GetUserRankObject(1021, 109, 3000, 2006)
};
Console.WriteLine("Ranked: \n" + string.Join("\n", ranked.Select(r => $"{r.Rank}. {r.UserId} ({r.AverageScore})")));
/*
Ranked:
1. 1022 (77.5)
2. 1021 (51.66667)
*/
public GetCalculatePerform(int? Student_ID, int? CourseID, int? SemID, int? SubjectID)
{           
    var scoreCard = dbcontext.Stu_Result
    .Where(u => u.CourseID == CourseID && u.SemID == SemID && u.SubjectID == SubjectID )
    .ToList()
    .GroupBy(u => u.UserID)
    .OrderByDescending(grp => grp.Average(u => u.ScoredMarks))
    .Select((grp, i) => new
    {
        UserId = grp.Key,
        Rank = i + 1,
        AverageScore = grp.Average(u => u.ScoredMarks)
    }).ToList().Single(u => u.UserId == Student_ID);
}
 public class GetCalculatePerform
    {

 abcEntities dbcontext = new abcEntities();

        public UserRankObject tempobj { get; set; }


        //********************************************************Performance & Rank Calculation********************************************


      public  GetCalculatePerform(int? Student_ID, int? CourseID, int? SemID, int? SubjectID)
        {


                var scoreCard = dbcontext.Stu_Result
                .Where(u => u.CourseID == CourseID && u.SemID == SemID && u.SubjectID == SubjectID)
                .ToList()
                .GroupBy(u => u.UserID)
                .OrderByDescending(grp => grp.Average(u => u.ScoredMarks))
                .Select((grp, i) => new UserRankObject
                {
                    UserId = grp.Key,
                    Rank = i + 1,
                    AverageScore = grp.Average(u => u.ScoredMarks)
                }).ToList().Single(u => u.UserId == Student_ID);
                // .FirstOrDefault(u=>u.UserId == Student_ID);


                this.tempobj = scoreCard;


      }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM