因此,目前我在正常工作的API中返回了此对象的列表。
public class ShoppingListItemDto
{
public int ShoppingListItemId { get; set; }
public string Name { get; set; }
public string Category { get; set; }
}
我想将其放入分组列表对象中。像这样:
public class GroupedShoppingListItemDto
{
public string Category { get; set; }
public List<ShoppingListItemDto> ShoppingListItemDto { get; set; }
}
I found this post and put together the below, but I can't seem to get it to pull in the nested objects.
var list = _context.ShoppingListItems
.GroupBy(sli => new { sli.Category })
.Select(sli => new GroupedShoppingListItemDto { }).ToList();
例如,我希望linq查询返回以下内容:
{
"category":"produce",
"items": [
{
"id":1,
"name":"lettuce",
"category":"produce"
},
{
"id":4,
"name":"cucumber",
"category":"produce"
}
],
"category":"meat",
"items": [
{
"id":2,
"name":"chicken",
"category":"meat"
},
{
"id":3,
"name":"steak",
"category":"meat"
}
]
}
You need to
ToList
onSelect
query.应该可以的