internal virtual bool Validate(StringBuilder validationMessages) { var retval = true; retval &= Incomes.OfType <IModelValidate>().ToList().All(i => i.Validate(validationMessages)); retval &= Expenses.OfType <IModelValidate>().ToList().All(e => e.Validate(validationMessages)); if (Expenses.Any(e => e.Bucket.Code == SurplusBucket.SurplusCode)) { validationMessages.AppendFormat(CultureInfo.CurrentCulture, "You can not use SURPLUS as an expense code."); retval = false; } IEnumerable <string> duplicates = Expenses .GroupBy(i => i.Bucket.Code) .Where(g => g.Count() > 1) .Select(g => g.Key); foreach (var duplicateCode in duplicates) { retval = false; validationMessages.AppendFormat(CultureInfo.CurrentCulture, "Expense {0} is listed multiple time, each bucket must have a different name.", duplicateCode); } return(retval); }