Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 67111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:12:31+00:00 2026-05-10T19:12:31+00:00

Assuming a table of tags like the stackoverflow question tags: TagID (bigint), QuestionID (bigint),

  • 0

Assuming a table of tags like the stackoverflow question tags:

TagID (bigint), QuestionID (bigint), Tag (varchar)

What is the most efficient way to get the 25 most used tags using LINQ? In SQL, a simple GROUP BY will do:

SELECT Tag, COUNT(Tag) FROM Tags GROUP BY Tag 

I’ve written some LINQ that works:

var groups = from t in DataContext.Tags              group t by t.Tag into g              select new { Tag = g.Key, Frequency = g.Count() }; return groups.OrderByDescending(g => g.Frequency).Take(25); 

Like, really? Isn’t this mega-verbose? The sad thing is that I’m doing this to save a massive number of queries, as my Tag objects already contain a Frequency property that would otherwise need to check back with the database for every Tag if I actually used the property.

So I then parse these anonymous types back into Tag objects:

groups.OrderByDescending(g => g.Frequency).Take(25).ToList().ForEach(t => tags.Add(new Tag() {     Tag = t.Tag,     Frequency = t.Frequency })); 

I’m a LINQ newbie, and this doesn’t seem right. Please show me how it’s really done.

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:12:32+00:00Added an answer on May 10, 2026 at 7:12 pm

    I’m pretty sure you’ve got it right. And, the SQL that LINQ generates and will send to your db will look just like the SQL you started with, so while you’re doing a bit more typing, your database isn’t doing any more work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which HTML tags would you use to describe table like this: +--------+---------+---------+---------+ | TH1
Assuming you have a simple table like the following: +---------+---------------------+ | user_id | activity_date
Assuming I have a table Entities with the columns Value and Type as varchar(strings)
assuming we have a table like below name | userid | score | datestamp
Assuming two columns in a MySQL (MyISAM) table: title VARCHAR(1500) url VARCHAR(155) I want
Assuming that I have the below customer_offer table. My question is: How to select
Assuming a table like this. id town_id begin_date 12 2 2011-10-10 23 2 2011-11-10
Assuming an InnoDB table, will the following command ... CHECK TABLE table_name; ... cause
my question is: assuming we have a calendar_table with UNIX datestamp date_column, i want
Assuming a table with a column where integers are stored. ----------------------------- id | some_int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.