IList<Customer> Customers =
flat.GroupBy(cust => new { cust.ReferenceNumber, cust.Name, cust.Address })
.Select(c => new Customer()
{
ReferenceNumber = c.Key.ReferenceNumber,
Name = c.Key.Name,
Address = c.Key.Address,
Orders = c.Select(o => new Order()
{
OrderId = o.OrderId,
ProductName = o.ProductName,
Description = o.Description,
Amount = o.Amount
}).ToList()
}).ToList()
Is taking a flat list and converting it into a nested object possible in Javascript?
A generic solution that is??
Yes.
You can pass functions around in JavaScript, which would serve the same purpose as the lambda expressions in the C# code. Take the use of JQuery’s “each” as an example:
You can also create nested objects quite easily:
Of course, you can do that dynamically, rather than all at once:
Then, to do what you’re looking for, you’ll need to use arrays: