简体   繁体   English

使用联接查询后,EF核心包含子项

[英]EF Core Include childs after query with joins

I am using .Net Core 2.0 and EF Core. 我正在使用.Net Core 2.0和EF Core。 The following query is working fine but does not include the children. 以下查询工作正常,但不包含子级。

What this query does is join: 该查询的作用是加入:

  • Order 订购
  • OrderItem and OrderItem和
  • Product 产品

By multiplying the Quantity (in OrderItem) with the price (in Product), and doing the sum of this, I get the order total. 通过将数量(在OrderItem中)与价格(在产品中)相乘,然后求和,得出订单总数。

Now I am trying to include "OrderItem" and "Product" as I would like to see those in the final JSON of my web API. 现在,我想在我的Web API的最终JSON中看到“ OrderItem”和“ Product”。 So getting the final JSON with all three entities and the summary of the total price for the order. 因此,要获得包含所有三个实体的最终JSON以及订单总价的摘要。

This is my repository: 这是我的存储库:

public class OrderRepository : IOrderRepository
{
    private readonly BasketDbContext _basketDbContext;
    public OrderRepository(BasketDbContext basketDbContext)
    {
        _basketDbContext = basketDbContext;
    }

    public Order GetOrderById(int id)
    {
        return (from o in _basketDbContext.Orders
                join oi in _basketDbContext.OrderItems on new { o.Id } equals new { Id = oi.OrderId }
                join p in _basketDbContext.Products on new { oi.ProductId } equals new { ProductId = p.Id }
                where o.Id == id
                group new { o, p, oi } by new
                {
                    o.Id,
                    o.OrderDate
                }
            into g
                select new Order
                {
                    Id = g.Key.Id,
                    OrderDate = g.Key.OrderDate,
                    TotalPrice = g.Sum(p => p.p.Price * p.oi.Quantity)
                })
            .Include(x => x.OrderItems)
            .ThenInclude(y => y.Product)
            .FirstOrDefault();
    }
}

The includes in the method GetOrderById is not working, but I don't know how to include these in such query. 方法GetOrderById中的include不起作用,但是我不知道如何在此类查询中包括这些。

These are my models: 这些是我的模型:

public class Order
{
    public int Id { get; set; }
    public DateTime OrderDate { get; set; }
    public decimal TotalPrice { get; set; }
    public List<OrderItem> OrderItems { get; set; }

    public decimal CalculateTotal()
    {
        return OrderItems.Sum(item => item.GetPrice());
    }
}

public class OrderItem
{
    public int Id { get; set; }
    [Required(ErrorMessage = "Empty OrderId")]
    public int OrderId { get; set; }
    [Required(ErrorMessage = "Empty ProductId")]
    public int ProductId { get; set; }
    [Required(ErrorMessage = "Empty Quantity")]
    [Range(1, 20, ErrorMessage = "Quantity must be between 1 to 20")]
    public int Quantity { get; set; }
    public Product Product { get; set; }

    public decimal GetPrice()
    {
        return Product.Price * Quantity;
    }
}

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
}

Thanks! 谢谢!

The data you are getting in order is what you select in query. 您要获得的数据是您在查询中选择的数据。 ie Id, OrderDate and TotalPrice: 即ID,OrderDate和TotalPrice:

select new Order
                {
                    Id = g.Key.Id,
                    OrderDate = g.Key.OrderDate,
                    TotalPrice = g.Sum(p => p.p.Price * p.oi.Quantity)
                })

If you want to map new order here (like id, orderDate etc) then you have to map "ItemOrder" and "Product" entities field by field as well. 如果要在此处映射新订单(如id,orderDate等),则还必须逐字段映射“ ItemOrder”和“ Product”实体。

I think you can simply return order without creating new order in query like: 我认为您可以简单地返回订单,而无需在查询中创建新订单,例如:

return (from o in _basketDbContext.Orders where o.Id == id select o)
       .Include(x => x.OrderItems)
       .ThenInclude(y => y.Product)
       .FirstOrDefault();

TotalPrice calculation can be done on returned result. 可以对返回的结果进行TotalPrice计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM