Entity Framework - GroupBy

Mahesh Kumar 65 Reputation points
2024-12-20T06:21:56.9233333+00:00

Hi

I have a quiz and questions model.

context.Quizzes.Include(Questions)
.Where(q => q.Status = true)
.GroupBy() //need group by Questions.QuizId and get Id, Title, Status, Total Questions in the Select
.Take(10).
.ToList();
GroupBy(quiz => quiz.Question.GroupBy(question => question.QuizId))
Is that valid?
How do i select all the column from quiz with total questions ?
Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
772 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hongrui Yu-MSFT 4,040 Reputation points Microsoft Vendor
    2024-12-20T08:46:56.1266667+00:00

    Hi, @Mahesh Kumar. Welcome to Microsoft Q&A. 

    Here we assume your table is as follows:

        public class Question
        {
            public int Id { get; set; }
            public int QuizId { get; set; }
    
            public Quiz Quiz { get; set; }
        }
    
        public class Quiz
        {
            public int Id { get; set; }
    
            public string Title { get; set; }
    
            public bool Status { get; set; }
    
            public List<Question> Question { get; set; }
        }
    

    The configuration information is as follows:

            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Quiz>(entity =>
                {
                    entity.HasKey(p=>p.Id);
    
                    entity.HasMany(p=>p.Question).WithOne(q=>q.Quiz).HasForeignKey(o=>o.QuizId);
                });
    
                modelBuilder.Entity<Question>(entity =>
                {
                    entity.HasKey(p => p.Id);
                });
            }
    

    If QuizId is used as a foreign key in question table and is associated with quiz's primary key Id, there is no need to group by QuizId, because the grouping operation is equivalent to grouping by quiz's id, but Id is unique.

    You could refer to the following query code to select

    var list = context.Quizzes.Include(p=>p.Question).Where(q=>q.Status == true).Select(p =>
        new 
        {
            Id = p.Id,
            Title = p.Title,
            Status = p.Status,
            TotalQuestions = p.Question.Count()
        }
    ).Take(10).ToList();
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.