Created
October 3, 2019 19:26
-
-
Save Defcoq/9b7aa4660b31de0bb60524c8223d5076 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TagCloudViewComponent : ViewComponent | |
{ | |
private readonly DataContext _context; | |
public TagCloudViewComponent() | |
{ | |
_context = new DataContext(); ; | |
} | |
public IViewComponentResult Invoke() | |
{ | |
var items = (from songs in _context.GetSong() | |
join genres in _context.GetAllGenre() on songs.GenreId equals | |
genres.GenreLookUpId | |
where songs.GenreId.HasValue | |
select new { genres.GenreName, songs.GenreId }).ToList(); | |
var grouped = items.GroupBy(s => s.GenreName).Select( | |
gen => new { Name = gen.Key, Count = gen.Count() }); | |
var model = new TagCloud(); | |
foreach (var item in grouped) | |
{ | |
var cti = new TagCloudItem | |
{ | |
DisplayText = item.Name, | |
Url = string.Concat("/Genres/", item.Name), | |
Weight = item.Count | |
}; | |
model.AddItem(cti); | |
} | |
return View(model); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment