public static IngredientSection[] GetSections(Recipe recipe) { //TODO: This code can probably be done in a LINQ expression, or more efficiently. var nullSection = new IngredientSection(null); var map = new Dictionary <string, IngredientSection>(); foreach (var usage in recipe.Ingredients) { if (String.IsNullOrEmpty(usage.Section)) { nullSection.Ingredients.Add(usage); } else { var sectionKey = usage.Section.ToLower(); IngredientSection sectionList; if (map.TryGetValue(sectionKey, out sectionList)) { sectionList.Ingredients.Add(usage); } else { sectionList = new IngredientSection(usage.Section); sectionList.Ingredients.Add(usage); map.Add(sectionKey, sectionList); } } } var ret = new List <IngredientSection>(); if (nullSection.Ingredients.Count > 0) { ret.Add(nullSection); } ret.AddRange(map.Values); return(ret.ToArray()); }
public static IngredientSection[] GetSections(Recipe recipe) { //TODO: This code can probably be done in a LINQ expression, or more efficiently. var nullSection = new IngredientSection(null); var map = new Dictionary<string, IngredientSection>(); foreach (var usage in recipe.Ingredients) { if (string.IsNullOrEmpty(usage.Section)) { nullSection.Ingredients.Add(usage); } else { var sectionKey = usage.Section.ToLower(); IngredientSection sectionList; if (map.TryGetValue(sectionKey, out sectionList)) { sectionList.Ingredients.Add(usage); } else { sectionList = new IngredientSection(usage.Section); sectionList.Ingredients.Add(usage); map.Add(sectionKey, sectionList); } } } var result = new List<IngredientSection>(); if (nullSection.Ingredients.Count > 0) { result.Add(nullSection); } result.AddRange(map.Values); return result.ToArray(); }