EF核心包括孩子的查询与联接后

问题描述 投票:0回答:1

我使用的.Net核2.0和EF核心。下面的查询工作正常,但不包括儿童。

这是什么查询会为加盟:

  • 订购
  • OrderItem的和
  • 产品

通过数量(以OrderItem的)的价格(在产品)相乘,做这个的总和,我得到的订单总额。

现在,我想包括“OrderItem的”和“产品”,因为我想看到那些在我的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不工作的包括,但我不知道如何在这样的查询包括这些。

这是我的模型:

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

谢谢!

c# linq .net-core asp.net-core-webapi ef-core-2.0
1个回答
3
投票

你为了所得到的数据就是你选择的查询。即编号,订购日期和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”和“产品”实体字段映射为好。

我想,你可以简单地没有像查询创造新的顺序返回命令:

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

TotalPrice计算可以在返回的结果来完成。

© www.soinside.com 2019 - 2024. All rights reserved.