Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. public class TagCloudViewComponent : ViewComponent
  2. {
  3. private readonly DataContext _context;
  4. public TagCloudViewComponent()
  5. {
  6. _context = new DataContext(); ;
  7. }
  8.  
  9. public IViewComponentResult Invoke()
  10. {
  11. var items = (from songs in _context.GetSong()
  12. join genres in _context.GetAllGenre() on songs.GenreId equals
  13. genres.GenreLookUpId
  14. where songs.GenreId.HasValue
  15. select new { genres.GenreName, songs.GenreId }).ToList();
  16.  
  17. var grouped = items.GroupBy(s => s.GenreName).Select(
  18. gen => new { Name = gen.Key, Count = gen.Count() });
  19.  
  20.  
  21. var model = new TagCloud();
  22.  
  23.  
  24. foreach (var item in grouped)
  25. {
  26. var cti = new TagCloudItem
  27. {
  28. DisplayText = item.Name,
  29. Url = string.Concat("/Genres/", item.Name),
  30. Weight = item.Count
  31. };
  32.  
  33. model.AddItem(cti);
  34. }
  35. return View(model);
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement