有人可以通过代码将这个组变成 Pomelo Entity Framework 吗?

Can someone Turn this group by code into Pomelo Entity Framework?

我想获取每天执行的任务数并将它们绘制到图表中,但问题是我无法弄清楚 pomelo 实体代码。

 Select  FirstName,count(ToDoId)
    From todos as M 
    Inner join users as u
        on M.UserId=u.UserId
    where u.UserId=1
    GROUP BY M.UserId, CAST(M.CreatedAt AS DATE)

假设对应 model classes for your two tables, that contain navigation properties,LINQ 查询可能如下所示:

var todoCountByFirstNameAndDate = context.Todos.Include(e => e.User)
    .GroupBy(t => new {t.User.FirstName, t.CreatedAt.Date})
    .Select(g => new {g.Key.FirstName, g.Key.Date, TodoCount = g.Count()})
    .ToList();

生成的 SQL 将是:

SELECT `u`.`FirstName`, CONVERT(`t`.`CreatedAt`, date) AS `Date`, COUNT(*) AS `TodoCount`
FROM `Todos` AS `t`
INNER JOIN `Users` AS `u` ON `t`.`UserId` = `u`.`UserId`
GROUP BY `u`.`FirstName`, CONVERT(`t`.`CreatedAt`, date)

这是一个完整且有效的控制台示例项目,它演示了细节:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;

namespace IssueConsoleTemplate
{
    //
    // Entities:
    //
    
    public class User
    {
        public int UserId { get; set; }
        public string FirstName { get; set; }
        
        public virtual ICollection<Todo> Todos { get; set; } = new HashSet<Todo>();
    }

    public class Todo
    {
        public int TodoId { get; set; }
        public DateTime CreatedAt { get; set; }
        public int UserId { get; set; }

        public User User { get; set; }
    }
    
    //
    // DbContext:
    //
    
    public class Context : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Todo> Todos { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder
                .UseMySql(
                    "server=127.0.0.1;port=3306;user=root;password=;database=So67149928",
                    b => b.ServerVersion("8.0.21-mysql")
                          .CharSetBehavior(CharSetBehavior.NeverAppend))
                .UseLoggerFactory(
                    LoggerFactory.Create(
                        b => b
                            .AddConsole()
                            .AddFilter(level => level >= LogLevel.Information)))
                .EnableSensitiveDataLogging()
                .EnableDetailedErrors();
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>()
                .HasData(
                    new User {UserId = 1, FirstName = "John"},
                    new User {UserId = 2, FirstName = "Jane"});
            
            modelBuilder.Entity<Todo>()
                .HasData(
                    new Todo {TodoId = 11, CreatedAt = new DateTime(2021, 4, 17, 14, 21, 41), UserId = 1},
                    new Todo {TodoId = 12, CreatedAt = new DateTime(2021, 4, 17, 18, 11, 21), UserId = 1},
                    new Todo {TodoId = 13, CreatedAt = new DateTime(2021, 4, 17, 14, 21, 41), UserId = 2},
                    new Todo {TodoId = 14, CreatedAt = new DateTime(2021, 4, 18, 18, 11, 21), UserId = 2});
        }
    }

    internal static class Program
    {
        private static void Main()
        {
            using var context = new Context();

            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            var todoCountByFirstNameAndDate = context.Todos.Include(e => e.User)
                .GroupBy(t => new {t.User.FirstName, t.CreatedAt.Date})
                .Select(g => new {g.Key.FirstName, g.Key.Date, TodoCount = g.Count()})
                .OrderBy(r => r.FirstName)
                    .ThenBy(r => r.Date)
                .ToList();

            Trace.Assert(todoCountByFirstNameAndDate.Count == 3);

            Trace.Assert(todoCountByFirstNameAndDate[0].FirstName == "Jane");
            Trace.Assert(todoCountByFirstNameAndDate[0].Date == new DateTime(2021, 4, 17));
            Trace.Assert(todoCountByFirstNameAndDate[0].TodoCount == 1);

            Trace.Assert(todoCountByFirstNameAndDate[1].FirstName == "Jane");
            Trace.Assert(todoCountByFirstNameAndDate[1].Date == new DateTime(2021, 4, 18));
            Trace.Assert(todoCountByFirstNameAndDate[1].TodoCount == 1);

            Trace.Assert(todoCountByFirstNameAndDate[2].FirstName == "John");
            Trace.Assert(todoCountByFirstNameAndDate[2].Date == new DateTime(2021, 4, 17));
            Trace.Assert(todoCountByFirstNameAndDate[2].TodoCount == 2);
        }
    }
}