Skip to content

Instantly share code, notes, and snippets.

@ryanlangton
Created May 16, 2012 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanlangton/2713537 to your computer and use it in GitHub Desktop.
Save ryanlangton/2713537 to your computer and use it in GitHub Desktop.
BETTER STILL --
private FaqTypeViewModel GetGeneralFaqType(IEnumerable<FaqModel> models)
{
return new FaqTypeViewModel
{
Name = "General",
Categories = GetCategoriesInModel(models)
};
}
private IEnumerable<FaqCategoryViewModel> GetCategoriesInModel(IEnumerable<FaqModel> models)
{
foreach (var category in faqRepository.GetAllCategories())
if (models.Any(x => x.CategoryKey == category.Key))
yield return BuildCategoryViewModel(category, models.Where(x => x.CategoryKey == category.Key));
}
=========================================================
OLD CODE --
private FaqTypeViewModel GetGeneralFaqType(IEnumerable<FaqModel> models)
{
var categories = new List<FaqCategoryViewModel>();
foreach (var category in faqRepository.GetAllCategories())
if(models.Any(x => x.CategoryKey == category.Key))
categories.Add(BuildCategoryViewModel(category, models.Where(x => x.CategoryKey == category.Key)));
return new FaqTypeViewModel
{
Name = "General",
Categories = categories
}
}
==== RYAN'S HORRIBLE CODE ====
private FaqTypeViewModel GetGeneralFaqType(IEnumerable<FaqModel> models)
{
var categories = new List<FaqCategoryViewModel>();
var type = new FaqTypeViewModel
{
Name = "General"
};
var categoryModels = faqRepository.GetAllCategories();
foreach (var category in categoryModels)
{
var faqs = models.Where(x => x.CategoryKey == category.Key);
if(faqs.Count() > 0)
{
categories.Add(BuildCategoryViewModel(category, faqs));
}
}
type.Categories = categories;
return type;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment