Click here to Skip to main content
15,877,967 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
I have an ASP.NET Core MVC app that contains two entities: Employee
and Task. The relationship between these two entities is One-to-many.
The information stored for an employee is the full name, email, phone
number, date of birth, and monthly salary. The task consists of a
title, description, assignee(the employee assigned to work on it), and a due date. I need to display the 5 employees who completed the largest number of tasks in the past month.

What I have tried:

public async Task<IActionResult> Display()
            int current_month = DateTime.Now.Month;

            var employees = _context.Employees.Include(t => t.EmployeeTasks).OrderByDescending(x => x.EmployeeTasks.Count(x => x.DueDate.Month == current_month - 1)).Take(5);

            return View(employees);

This query is not working properly, as if it does not recognize the condition
DueDate.Month == current_month - 1

Does anyone know how to correct the given query?
Updated 11-Mar-23 18:58pm
Graeme_Grant 11-Mar-23 19:55pm    
I'm not doing your homework for you. Try googling, this is a very common question and there are many answers available to you. You just need to try.

The query does work. It's doing exactly what you told it to do. The problem you have is that it's your understanding of the query that isn't correct.
Share this answer
The first thing to note is that that simplistic test will not work in January ... it will not roll back to December!

Have a look at this: DateTime.AddMonths(Int32) Method (System) | Microsoft Learn[^] and think about what you actually need to compare.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900