{
'orders' : [
{
id: 1,
sku: 'AR',
sum: 100
},
{
id: 2,
sku: 'PR',
sum: 200
},
{
id: 1,
sku: 'VR',
sum: 150
},
]
}
class Program
{
static void Main(string[] args)
{
List<Order> orders = new List<Order>()
{
new Order()
{
id = 1,
sku = "AR",
sum = 100
},
new Order()
{
id = 2,
sku = "PR",
sum = 200
},
new Order()
{
id = 1,
sku = "VR",
sum = 150
}
};
var groupedOrders = orders.GroupBy(el => el.id)
.Select(el => new Order()
{
id = el.Key,
sum = el.Sum(s => s.sum),
sku = String.Join(String.Empty, el.Select(str => str.sku))
});
}
}
public class Order
{
public int id;
public string sku;
public int sum;
}
class Order
{
public int id;
public string sku;
public int sum;
}
class Program
{
static void Main(string[] args)
{
List<Order> orders = new List<Order>()
{
new Order()
{
id = 1,
sku = "AR",
sum = 100
},
new Order()
{
id = 2,
sku = "PR",
sum = 200
},
new Order()
{
id = 1,
sku = "VR",
sum = 150
}
};
var result = orders.GroupBy(i => i.id).
Select(i => new
{
id = i.Key,
sum = i.Sum(s => s.sum),
sku = i.Select(s => s.sku)
});
}
}
class Program
{
static void Main(string[] args)
{
string rawJson = File.ReadAllText("data.json"); // for example
OrderCollection orders = JsonConvert.DeserializeObject<OrderCollection>(rawJson);
foreach (var item in orders.Orders.GroupBy(x => x.Id))
{
Console.WriteLine($"Order id: {item.Key}, sum: {item.Sum(x => x.Sum)}");
}
}
}
class OrderCollection
{
public List<Order> Orders { get; set; }
}
class Order
{
public int Id { get; set; }
public string Sku { get; set; }
public int Sum { get; set; }
}