簡體   English   中英

如何在 LINQ 中執行以下查詢?

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

我想用 LINQ 執行以下查詢:

  • 所有單位的計數
  • 所有單元得分和總分的總和
  • 平均值:Scoremarks%Totalmarks*100 4) 排名
  • Rank 基於分數(如果用戶 1 完成了 2 個單元,單元 id 為 100 和 101,則應與其他用戶進行比較)

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

代碼:

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
}

您改編的函數以及我引入的用於存儲結果的額外類:

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

用法:

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