【发布时间】:2022-02-07 23:16:32
【问题描述】:
以下查询在字典数组中返回子孙的 ItemNo,因此关系得以保持。
public async Task<List<KeyValuePair<string, IEnumerable<string>>>> GetChild(string parentItemNo)
{
var childDetails = await DbContext.Items
.Where(x => x.ItemNo== parentItemNo)
.SelectMany(x => x.Children.Select(c => new
{
c.ItemNo, // I can select c.ItemName as well
// how do I select ItemName below, tried Select(gc => new { gc.ItemNo, gc.ItemName})
GrandChildItemNos = c.Children.Select(gc => gc.ItemNo)
})).ToListAsync();
var dictionary = childDetails.ToDictionary(d => d.ItemNo, d => d.GrandChildItemNos).ToList(); // how do i set ItemName here
return dictionary;
}
查询返回 children(key) 和 grandchild(value),如下所示
[{"Key":"A","Value":["AQ11", AH45]},{"Key":"C","Value":["CN22", "CL33", "CG24"]}]
如何为每个子孙返回“ItemName”。
【问题讨论】:
标签: sql linq entity-framework-core asp.net-core-webapi