public static List <ShippingGroup> ToModel(this IQueryable <shippingRule> rules, Currency currency, bool ismetric) { // sort first var ordered = rules.OrderBy(x => x.country).ThenBy(x => x.state); var groups = new List <ShippingGroup>(); int? countryid = null; string statename = ""; ShippingGroup countrygroup = null; ShippingGroup stategroup = null; foreach (var rule in ordered) { string countryname = "Everywhere Else"; if (rule.country.HasValue) { var topcountry = Country.GetCountry(rule.country.Value); countryname = topcountry.name; } // if countryid is null then this is the first time // if not the same then is new country group if (!countryid.HasValue || rule.country != countryid.Value) { /////////// new country countrygroup = new ShippingGroup(); countrygroup.name = countryname; groups.Add(countrygroup); statename = ""; } countryid = rule.country; if (string.IsNullOrEmpty(rule.state)) { ////////////// country level var shippingRule = new ShippingRule { id = rule.id.ToString(), cost = rule.cost.ToString("n" + currency.decimalCount), countryid = rule.country, country = countryname, matchValue = rule.matchvalue.ToString("n2"), name = rule.name, ruleType = (RuleType)rule.ruletype }; shippingRule.matchValue = rule.matchvalue.ToMatchValueDisplay(shippingRule.ruleType, currency, ismetric); countrygroup.AddRule(shippingRule); } else { if (string.IsNullOrEmpty(statename) || rule.state != statename) { ////// new state stategroup = new ShippingGroup(); stategroup.name = rule.state.ToStateName(countryid.Value.ToString()); statename = rule.state; countrygroup.AddSubGroup(stategroup); } /////////// state level var shippingRule = new ShippingRule { id = rule.id.ToString(), cost = rule.cost.ToString("n" + currency.decimalCount), countryid = rule.country, state = rule.state, matchValue = rule.matchvalue.ToString("n2"), name = rule.name, ruleType = (RuleType)rule.ruletype }; shippingRule.country = "Everywhere Else"; if (rule.country.HasValue) { shippingRule.country = Country.GetCountry(rule.country.Value).name; } shippingRule.matchValue = rule.matchvalue.ToMatchValueDisplay(shippingRule.ruleType, currency, ismetric); stategroup.AddRule(shippingRule); } } return(groups); }
public void AddSubGroup(ShippingGroup group) { subgroup.Add(group); }