So I have a list of this object returned in a working API at the moment.
public class ShoppingListItemDto
{
public int ShoppingListItemId { get; set; }
public string Name { get; set; }
public string Category { get; set; }
}
I want to get it into a grouped list object. Something like this:
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();
For example, I'd like the linq query to return something like this:
{
"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"
}
]
}