I have the following objects:
Subscription List Account Class:
public class Account {
subscription class with a list of options and additions:
public class Subscription { /// <summary> /// Subscription ID /// </summary> public string ID { get; set; } /// <summary> /// Quantity of the subscription. /// </summary> public int Quantity { get; set; } /// <summary> /// A list with all subscription add ons /// </summary> public IEnumerable<AddOn> AddOns { get; set; } /// <summary> /// A List with all subscription variations /// </summary> public IEnumerable<Variation> Variations { get; set; } }
add class with a list of options:
public class AddOn { /// <summary> /// Gets or Sets add on id /// </summary> public string ID { get; set; } /// <summary> /// Quantity of the add on. /// </summary> public int Quantity { get; set; } /// <summary> /// A List with all add on variations /// </summary> public IEnumerable<Variation> Variations { get; set; } }
And the variation class:
public class Variation { /// <summary> /// Variation ID /// </summary> public string ID { get; set; } /// <summary> /// offerUri /// </summary> public string Code { get; set; } /// <summary> /// Variation Value /// </summary> public string Value { get; set; } /// <summary> /// Variation Name /// </summary> public string Name { get; set; } }
What I'm trying to do is group all the add-ons with a specific code and summarize the amount. For example, I tried:
var groupAddOnsByCode = acc.Subscriptions.Select(s => s.AddOns.GroupBy(a => a.Variations.Select(v => v.Code).FirstOrDefault())).ToList();
This is one correct addition of groups, but I need a list with the addition of a subscription for each group by code and total amount per code.
For example, if a subscription has an X-number of add-ons, and the code of each add-on is 1, 2, ..., X, I want to group using add-ons by code and total number using this code. I expect the result to be something like if I have the following structure:
Pseudo-code with the current structure ( The code belongs to the Variation class, which everyone adds has):
Subscription {
What I expect:
Subscription { AddOn1 { Variation = { Code = 1 }, Quantity = totalAmountOfQuantityForAddOnsWithCode = 1 }, ... AddOnX { Variation = { Code = X }, Quantity = totalAmountOfQuantityForAddOnsWithCode = X }, }
You can use this dotnetfiddle to check for dummy data.
Sorry for the long post and I will appreciate any help. Also keep in mind that my knowledge of C # and Linq is limited.