public IList<Pattern> GetPatternsFor(Insulin insulin, Meal meal, Sugar currentBefore = null) { var patterns = new List<Pattern>(); finder = factories.Finder; settings = factories.Settings; searchedInsulin = insulin; searchedMeal = meal; searchedItems = meal.NormalizedItems(); searchedSugarBefore = currentBefore; foreach (var testMeal in factories.Meals.Where(m => m != searchedMeal)) { var mealHasMatch = testMeal.Items.Any(item => searchedItems.Any(searchedItem => item.ProductId == searchedItem.ProductId && item.Unit == searchedItem.Unit)); if (mealHasMatch) foreach (var item in testMeal.NormalizedItems()) foreach (var searchedItem in searchedItems) if (item.ProductId == searchedItem.ProductId && item.Unit == searchedItem.Unit) { this.searchedItem = searchedItem; this.item = item; this.meal = testMeal; if (ConsiderPattern()) patterns.Add(BuildPattern()); } } return patterns; }