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

时间:2019-02-04 01:24:48

标签: c# linq .net-core asp.net-core-webapi ef-core-2.0

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

此查询的作用是加入:

  • 订购
  • OrderItem和
  • 产品

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

现在,我想包括“ OrderItem”和“ Product”,因为我希望在我的Web API的最终JSON中看到它们。因此,获取具有所有三个实体的最终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; }
}

谢谢!

1 个答案:

答案 0 :(得分:3)

要获取的数据是您在查询中选择的数据。即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();

可以对返回的结果进行总价格计算。

相关问题