public ViewResult Archive(ArchiveFilter filter = ArchiveFilter.Alphabet)
        {
            ArchiveArticlesViewModel model = new ArchiveArticlesViewModel(null, null)
            {
                Filter = filter,
            };

            List <ArticleTitle> articles = articleRepository.GetArticlesTitles();

            model.ArticlesGroups = ArticleService.GroupArticles(articles, filter);

            return(View(model));
        }
示例#2
0
        public static List <ArticleTitleGroup> GroupArticles(List <ArticleTitle> articles, ArchiveFilter filter)
        {
            if (articles == null)
            {
                throw new ArgumentNullException("articles");
            }

            List <ArticleTitleGroup> articlesGroups = new List <ArticleTitleGroup>();

            if (articles.Count() == 0)
            {
                return(articlesGroups);
            }

            if (filter == ArchiveFilter.Alphabet)
            {
                var grouped = articles
                              .GroupBy(x => x.Title[0])
                              .OrderBy(x => x.Key)
                              .ToList();

                foreach (var group in grouped)
                {
                    articlesGroups.Add(new ArticleTitleGroup
                    {
                        Articles  = group.ToList(),
                        GroupName = group.Key.ToString(),
                    });
                }
            }
            else if (filter == ArchiveFilter.Date)
            {
                var grouped = articles
                              .OrderByDescending(x => x.CreationDate)
                              .GroupBy(x => x.CreationDate.Year)
                              .ToList();

                foreach (var group in grouped)
                {
                    articlesGroups.Add(new ArticleTitleGroup
                    {
                        Articles  = group.ToList(),
                        GroupName = group.Key.ToString(),
                    });
                }
            }

            return(articlesGroups);
        }