如何在 asp.net api 响应中包含嵌套列表
How to include nested List in asp.net api response
我正在使用嵌套列表(List inside List)一切正常,但是当我尝试从 Api (GetOrders) 获得响应时,它没有显示列表。我尝试 .include(x=>x.ShopOrdersList) 但它只包括“ShopOrdersList”,而“ShopOrdersList”内的第二个列表“OrderSummaries”未显示。
我的模型是follow
public class Orders
{
[Key]
public int InvoiceNumber { get; set; }
public int ShopId { get; set; }
public string ShopName { get; set; }
public string Address { get; set; }
public string OwnerName { get; set; }
public string OwnerCnic { get; set; }
public string Area { get; set; }
public string PhoneNumber { get; set; }
public int TotalSale { get; set; }
public int TotalProfit { get; set; }
public int TotalRecover { get; set; }
public List<ShopOrder> ShopOrdersList { get; set; }
}
public class ShopOrder
{
[Key]
public int ShopOrderId { get; set; }
public DateTime BookingDate { get; set; }
public DateTime DeliveryDate { get; set; }
public string OrderBooker { get; set; }
public string DeliveryMan { get; set; }
public byte[] Signature { get; set; }
public int TotalQuantity { get; set; }
public bool IsDelivered { get; set; }
public bool IsAccepted { get; set; }
public bool IsPaymentAdded { get; set; }
public int TotalProfit { get; set; }
public int TotalPrice { get; set; }
public string UserId { get; set; }
public List<OrderSummary> OrderSummaries { get; set; }
}
public class OrderSummary
{
[Key]
public int OrderSummaryId { get; set; }
public int ProductId { get; set; }
public string ProductName { get; set; }
public string Detail { get; set; }
public int Price { get; set; }
public byte[] Image { get; set; }
public int PcsInCtn { get; set; }
public int OnePcsPurchasePrice { get; set; }
public int Ctns { get; set; }
public int Quantity { get; set; }
public int TotalPrice { get; set; }
}
控制器代码为
// GET: api/Orders
public IQueryable<Orders> GetOrders()
{
return db.Orders.Include(x => x.ShopOrdersList);
}
这是我收到的回复。
现在我想包括“OrderSummaries”列表。提前致谢。
检查这个答案EF LINQ include multiple and nested entities
它说你可以调用 ThenInclude
添加嵌套条目
public IQueryable<Orders> GetOrders()
{
return db.Orders.Include(x => x.ShopOrdersList).ThenInclude(y=>y.OrderSummaries);
}
我正在使用嵌套列表(List inside List)一切正常,但是当我尝试从 Api (GetOrders) 获得响应时,它没有显示列表。我尝试 .include(x=>x.ShopOrdersList) 但它只包括“ShopOrdersList”,而“ShopOrdersList”内的第二个列表“OrderSummaries”未显示。 我的模型是follow
public class Orders
{
[Key]
public int InvoiceNumber { get; set; }
public int ShopId { get; set; }
public string ShopName { get; set; }
public string Address { get; set; }
public string OwnerName { get; set; }
public string OwnerCnic { get; set; }
public string Area { get; set; }
public string PhoneNumber { get; set; }
public int TotalSale { get; set; }
public int TotalProfit { get; set; }
public int TotalRecover { get; set; }
public List<ShopOrder> ShopOrdersList { get; set; }
}
public class ShopOrder
{
[Key]
public int ShopOrderId { get; set; }
public DateTime BookingDate { get; set; }
public DateTime DeliveryDate { get; set; }
public string OrderBooker { get; set; }
public string DeliveryMan { get; set; }
public byte[] Signature { get; set; }
public int TotalQuantity { get; set; }
public bool IsDelivered { get; set; }
public bool IsAccepted { get; set; }
public bool IsPaymentAdded { get; set; }
public int TotalProfit { get; set; }
public int TotalPrice { get; set; }
public string UserId { get; set; }
public List<OrderSummary> OrderSummaries { get; set; }
}
public class OrderSummary
{
[Key]
public int OrderSummaryId { get; set; }
public int ProductId { get; set; }
public string ProductName { get; set; }
public string Detail { get; set; }
public int Price { get; set; }
public byte[] Image { get; set; }
public int PcsInCtn { get; set; }
public int OnePcsPurchasePrice { get; set; }
public int Ctns { get; set; }
public int Quantity { get; set; }
public int TotalPrice { get; set; }
}
控制器代码为
// GET: api/Orders
public IQueryable<Orders> GetOrders()
{
return db.Orders.Include(x => x.ShopOrdersList);
}
这是我收到的回复。
检查这个答案EF LINQ include multiple and nested entities
它说你可以调用 ThenInclude
添加嵌套条目
public IQueryable<Orders> GetOrders()
{
return db.Orders.Include(x => x.ShopOrdersList).ThenInclude(y=>y.OrderSummaries);
}