使用 INCLUDE(NOT JOIN)将 LINQ 转换为 LAMBDA Entity Framework Core

发布于 2025-01-16 14:18:19 字数 2308 浏览 1 评论 0原文

如何在 Entity Framework Core 中使用 Include 方法(而不是 Join 方法)调用多个实体?我正在尝试将此 LINQ 查询转换为 EF Core 5 语法,但我不知道如何调用多个实体并使用 include 方法将它们连接在一起。

        var reservations = from reservation in _dbContext.Reservations
                           join customer in _dbContext.Users on reservation.UserId equals customer.Id
                           join movie in _dbContext.Movies on reservation.MovieId equals movie.Id

                           select new
                           {
                               Id = reservation.Id,
                               ReservationTime = reservation.ReservationTime,
                               CustomerName = customer.Id,
                               MovieName = movie.Name
                           };

我尝试使用多个包含和选择方法,但不知道如何调用多个实体并加入

以下是我的模型

public class Reservation
{
    public int Id { get; set; }
    public int Qty { get; set; }
    public double Price { get; set; }
    public string Phone { get; set; }
    public DateTime ReservationTime { get; set; }
    public int MovieId { get; set; }
    public int UserId { get; set; }
}

public class Movie
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Language { get; set; }
    public string Duration { get; set; }
    public DateTime PlayingDate { get; set; }
    public DateTime PlayingTime { get; set; }
    public double TicketPrice { get; set; }
    public double Rating { get; set; }
    public string Genre { get; set; }
    public string TrailorUrl { get; set; }
    public string ImageUrl { get; set; }

    [NotMapped]
    public IFormFile Image { get; set; }
    public ICollection<Reservation> Reservations { get; set; }
}

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string Role { get; set; }
    public ICollection<Reservation> Reservations { get; set; }
}

控制器代码:

        var reservations = _dbContext.Reservations
            .Include(r => r.Id)
            .Include(c => c.User)
            .Select(x => new { x.Id, x.ReservationTime, x.User, x.User.Name });

How to call multiple entities using Include method (not Join method) in Entity Framework Core? I am trying to translate this LINQ query to EF Core 5 syntax, but I do not know how to call multiple entities and join them together using include method.

        var reservations = from reservation in _dbContext.Reservations
                           join customer in _dbContext.Users on reservation.UserId equals customer.Id
                           join movie in _dbContext.Movies on reservation.MovieId equals movie.Id

                           select new
                           {
                               Id = reservation.Id,
                               ReservationTime = reservation.ReservationTime,
                               CustomerName = customer.Id,
                               MovieName = movie.Name
                           };

I tried using multiple include and select method, but do not know how to call multiple entities and join

Here are my models

public class Reservation
{
    public int Id { get; set; }
    public int Qty { get; set; }
    public double Price { get; set; }
    public string Phone { get; set; }
    public DateTime ReservationTime { get; set; }
    public int MovieId { get; set; }
    public int UserId { get; set; }
}

public class Movie
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Language { get; set; }
    public string Duration { get; set; }
    public DateTime PlayingDate { get; set; }
    public DateTime PlayingTime { get; set; }
    public double TicketPrice { get; set; }
    public double Rating { get; set; }
    public string Genre { get; set; }
    public string TrailorUrl { get; set; }
    public string ImageUrl { get; set; }

    [NotMapped]
    public IFormFile Image { get; set; }
    public ICollection<Reservation> Reservations { get; set; }
}

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string Role { get; set; }
    public ICollection<Reservation> Reservations { get; set; }
}

Controller code:

        var reservations = _dbContext.Reservations
            .Include(r => r.Id)
            .Include(c => c.User)
            .Select(x => new { x.Id, x.ReservationTime, x.User, x.User.Name });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫七 2025-01-23 14:18:19

如果添加到 Reservation 导航属性 MovieUser,则可以简化您的查询。 Include 不能与 Select 一起使用,它会被 EF 翻译器忽略。

var reservations = _dbContext.Reservations
    .Select(r => new             
    {
        Id = r.Id,
        ReservationTime = r.ReservationTime,
        CustomerName = r.User.Id,
        MovieName = r.Movie.Name
    });

If add to Reservation navigation properties Movie and User, your query can be simplified. Include cannot be used with Select together, it is ignored by EF translator.

var reservations = _dbContext.Reservations
    .Select(r => new             
    {
        Id = r.Id,
        ReservationTime = r.ReservationTime,
        CustomerName = r.User.Id,
        MovieName = r.Movie.Name
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文