【发布时间】:2021-09-13 04:17:17
【问题描述】:
我在客户下订单的网页中工作。一个客户可以有很多订单,这意味着当我列出订单时,一些客户会出现两次。我按截止日期对客户进行分组,我明白了。问题是当我将 json 传递到前端时,值不显示。我想问题是因为在我的 get 方法中我没有传递我的对象类型,而在我的前端我需要它们但我不知道如何传递它们。
这就是我的page 的样子。如您所见,我得到了 John 两次,因为他有 2 个订单。
这是我在Get 方法中在 ASP.NET Core 中尝试的:
[Route("~/api/order")]
public IQueryable<Order> GetRegistrationSummaryAll()
{
var newList = _database.Orders
.Include(q => q.Client)
.GroupBy(x => new { x.DueDate })
.Select(y => new Order
{
Id = y.Max(x => x.Id),
ClientId = y.Max(x => x.ClientId),
InventoryId = y.Max(x => x.InventoryId),
Quantity = y.Max(x => x.Quantity),
Price = y.Max(x => x.Price),
LogoId = y.Max(x => x.LogoId),
DueDate = y.Key.DueDate
}
);
return newList;
}
我的模型是这个:
public class Order
{
public int Id { get; set; }
public int ClientId { get; set; }
public int InventoryId { get; set; }
public int Quantity { get; set; }
public double Price { get; set; }
public int LogoId { get; set; }
public DateTime DueDate { get; set; }
public Client Client { get; set; }
public Inventory Inventory { get; set; }
public Logo Logo { get; set; }
public List<Event> Events { get; set; }
}
更新
这是我从 get 方法组中得到的 Json 结果。如果您看到我的对象类型客户端,库存和徽标为空,因为我在 get 方法中没有发送任何内容:
[
{
"id": 1,
"clientId": 1,
"inventoryId": 1,
"quantity": 5,
"price": 100.0,
"logoId": 1,
"dueDate": "2021-09-13T00:00:00",
"client": null,
"inventory": null,
"logo": null,
"events": null
},
{
"id": 4,
"clientId": 2,
"inventoryId": 3,
"quantity": 10,
"price": 200.0,
"logoId": 2,
"dueDate": "2021-09-15T00:00:00",
"client": null,
"inventory": null,
"logo": null,
"events": null
}
]
【问题讨论】:
-
请分享正在提供的 JSON(Chrome 中的开发工具,网络选项卡)。
-
嘿,你的问题到底是什么?帮助会更容易:)
-
@Morasiu 问题是如何在我的选择中声明对象类型?
-
@mjwills 我现在发布 Json
-
ASP.NET Core 不会对数据进行分组或从数据库中读取任何内容。这是一个网络应用程序框架。你在使用像 EF Core 这样的 ORM 吗?哪个版本?
标签: c# json linq asp.net-core entity-framework-core