繁体   English   中英

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

[英]EF Core Include childs after query with joins

我正在使用.Net Core 2.0和EF Core。 以下查询工作正常,但不包含子级。

该查询的作用是加入:

  • 订购
  • OrderItem和
  • 产品

通过将数量(在OrderItem中)与价格(在产品中)相乘,然后求和,得出订单总数。

现在,我想在我的Web API的最终JSON中看到“ OrderItem”和“ Product”。 因此,要获得包含所有三个实体的最终JSON以及订单总价的摘要。

这是我的存储库:

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();
    }
}

方法GetOrderById中的include不起作用,但是我不知道如何在此类查询中包括这些。

这些是我的模型:

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; }
}

谢谢!

您要获得的数据是您在查询中选择的数据。 即ID,OrderDate和TotalPrice:

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

如果要在此处映射新订单(如id,orderDate等),则还必须逐字段映射“ ItemOrder”和“ Product”实体。

我认为您可以简单地返回订单,而无需在查询中创建新订单,例如:

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

可以对返回的结果进行TotalPrice计算。

暂无
暂无

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

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