Skip to content

Codepolice

  • ⤫

Create a tag cloud with a limited amount of tags in ASP.NET and LINQ

Posted by Judy Alvarez Posted on February 25, 2022March 1, 2022
0

Today I built a Tag cloud for my new site. I found this great article about how to do it with LINQ and C#. But I missed some features. Especially how to limit the number of tags that were shown. So if you read that article and then have a look at this code if you have the same problem as me.

var tagSummary = (from ti in db.TagItemRelations
	group ti by ti.Tag.Text into tagGroup
	select new {
	Tag = tagGroup.Key,
	TagFrequency = tagGroup.Count()
}).OrderByDescending(x=>x.TagFrequency).Take(20);

int maxTagFrequency = (from t in tagSummary select t.TagFrequency).Max();

var tagCloud = (from ti in tagSummary
	select new TagCloudItem {
	Tag = ti.Tag,
	Weight = (double)ti.TagFrequency / maxTagFrequency * 100
}).OrderBy(x=>x.Tag);

The main difference between my code and the code in the article is that I have a slightly more complex data model (should not matter) and that I make the second query against the “result” from the first query. This allows me to first order by popularity and then in alphabetic order.

Please leave a comment if there is any better way to do this or if you have any questions.

Categories: JavascriptTagged: asp . net interview questions, asp .net core, asp dot net, asp net 5, asp net ajax, asp net boilerplate, asp net cache, asp net caching, asp net core 5.0, asp net core webapi, asp net dropdownlist, asp net identity, asp net mvc 5, asp net nedir, asp net session management, asp net vs php, asp net web api interview questions, asp net webforms, asp net zero, asp. net, connection string in asp net, cookies asp net, framework asp net, interview questions on asp net, is asp net a framework, php vs asp net, query string in asp net, web services in asp net, what is asp net mvc

Post navigation

Previous Previous post: ASP.NET 4.0 – Some info from PDC
Next Next post: Using JQuery Validation plugin with ASP.NET

Related Posts

  • Playwright now offers a UI mode

    #​631 — March 24, 2023 Read on the Web JavaScript Weekly Speeding Up the JavaScript Ecosystem: npm Scripts — The latest in what has been a fascinating series on finding ‘low hanging fruit’ when it comes to performance in the JavaScript world. The author explains it best himself: “‘npm scripts’ are executed by JavaScript developers

    Posted by Posted on March 24, 2023
    0
  • Everyone’s coming for Node.js this week

    #​479 — March 23, 2023 Read on the Web 🔒  npm Granular Access Tokens Now Generally Available — The granular access token feature on the npm registry is now generally available, allowing you to restrict token access to specific packages, set expiration dates, limit access by IP range, and more. GitHub Automatic npm Publishing with GitHub

    Posted by Posted on March 23, 2023
    0
  • Transformers: JavaScript in Disguise

    #​630 — March 17, 2023 Read on the Web JavaScript Weekly 🤖  Transformers.js: Running ML Models in the Browser — Transformers are a type of machine learning model often used for natural language or visual processing and while running such models directly in the browser is in its infancy, Transformers.js opens up some ML models

    Posted by Posted on March 17, 2023
    0
  • Shell-free scripting from Node

    #​478 — March 16, 2023 Read on the Web Shell-Free Scripts with Execa 7.1 — Execa is a popular process execution library for Node and the latest version includes an interesting $ method feature for writing zx-style scripts with it, making it even more useful for shell scripting style usecases. ehmicky Turbowatch: File Change Detector and

    Posted by Posted on March 16, 2023
    0
  • New JavaScript features of the past few years

    #​629 — March 10, 2023 Read on the Web JavaScript Weekly JavaScript Features from the Past Few Years — Packed with examples, this post tackles the changes and tweaks to JavaScript and TypeScript over the past several years (some as far back as ES6/ES2015, like tagged template literals). Linus Schlumberger Astro’s 2023 Web Framework Performance Report —

    Posted by Posted on March 10, 2023
    0
  • Taking flight with Feathers 5

    #​477 — March 9, 2023 Read on the Web Feathers 5: The API and Real-Time App Framework — Feathers isn’t as well known as Nest or Fastify, say, but it’s a powerful and mature option if you want to spin up a Node CRUD app tied to a database and now it’s “TypeScript all the

    Posted by Posted on March 9, 2023
    0
Judy Alvarez

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Codepolice

  • Github
  • Atlassian
  • Flatlogic
  • Xero
  • Jetbrains
  • Figma
  • Playwright now offers a UI mode
  • Everyone’s coming for Node.js this week
  • Transformers: JavaScript in Disguise
  • Shell-free scripting from Node
  • New JavaScript features of the past few years
https://flatlogic.com/generator
COPYRIGHT © 2023 - Codepolice