Skip to content

Instantly share code, notes, and snippets.

@Defcoq
Created October 3, 2019 19:26
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 Defcoq/9b7aa4660b31de0bb60524c8223d5076 to your computer and use it in GitHub Desktop.
Save Defcoq/9b7aa4660b31de0bb60524c8223d5076 to your computer and use it in GitHub Desktop.
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