public async Task<ActionResult> Tag(string tag, int skip = 0, int order = 0) { if (tag == null) return View("Search",new SearchResultViewModel() { Articles = new SearchResultArticle[0],Order = order,Skip = skip}); var context = Request.GetOwinContext().Get<ApplicationDbContext>(); ArticleTagModel tagModel = context.Tags.Where(f => f.TagName.Equals(tag)).FirstOrDefault(); await context.Entry(tagModel).Collection(f=>f.Articles).LoadAsync(); SearchResultViewModel vm = new SearchResultViewModel(); List<SearchResultArticle> articles = new List<SearchResultArticle>(); var query = tagModel.Articles.AsQueryable(); var count = query.Count(); query=ChangeOrder(order, query); query=query.Skip(skip); foreach (var source in query.Take(10)) { articles.Add(new SearchResultArticle() { ArticleId = source.ArticleModelId, LabelCount = source.LabelCount, PageView = source.PageView, Title = source.Title }); } vm.Articles = articles.ToArray(); vm.SearchText = tag; vm.Order = order; vm.Skip = skip; vm.Count = count; if (vm.Articles.Length == 0) { vm.SearchResultText = string.Format("「{0}」タグがついている記事は見つかりませんでした。", tag); } else { vm.SearchResultText = string.Format("「{0}」タグのついている記事、検索結果{1}件中{2}~{3}件", tag, count, skip + 1, Math.Min(skip + 21, count)); } return View("Search", vm); }
public async Task<ActionResult> Search(string searchText,int skip=0,int order=0) { if(searchText==null)return View(new SearchResultViewModel() {Articles = new SearchResultArticle[0],Order = order,Skip = skip}); string[] queries=searchText.Split(' '); var context = Request.GetOwinContext().Get<ApplicationDbContext>(); string first = queries[0]; var result=context.Articles.Where((f) => f.Title.Contains(first)&&!f.IsDraft); for (int index = 1; index < Math.Min(4,queries.Length); index++) { var query = queries[index]; result=result.Where(f => f.Title.Contains(query)); } int count = await result.CountAsync(); result = ChangeOrder(order, result); result = result.Skip(skip); SearchResultViewModel vm=new SearchResultViewModel(); ArticleThumbnailManager thumbnailManager=new ArticleThumbnailManager(new BlobStorageConnection()); List<SearchResultArticle> articles=new List<SearchResultArticle>(); foreach (var source in result.Take(20)) { articles.Add(new SearchResultArticle() { ArticleId = source.ArticleModelId, LabelCount = source.LabelCount, PageView = source.PageView, Title = source.Title, Article_UpDate = source.UpdateTime.ToShortDateString(), ThumbnailTag = thumbnailManager.GenerateThumnailTag(source.ArticleModelId) }); } vm.Articles = articles.ToArray(); if (vm.Articles.Length == 0) { vm.SearchResultText = string.Format("「{0}」に関する検索結果は見つかりませんでした。", searchText); } else { vm.SearchResultText = string.Format("「{0}」に関する検索結果:{1}件中{2}~{3}件", searchText,count,skip+1,Math.Min(skip+21,count)); } vm.SearchText = searchText; vm.Order = order; vm.Skip = skip; vm.Count = count; return View(vm); }