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 6613435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:13:06+00:00 2026-05-25T20:13:06+00:00

Using Entity Framework 4.1 Code First I have two objects with a many-to-many relationship:

  • 0

Using Entity Framework 4.1 Code First I have two objects with a many-to-many relationship:

 public class Article  
    {
        public int ID { get; set; } 
        public string Title { get; set; } 
        public string Description { get; set; }
        public ICollection<Tag>  Tags { get; set; }
}

 public class Tag
        { 
            [Key]
            public string UrlSlug { get; set; }
            public string Name { get; set; }
            public ICollection<Article> Articles { get; set; }
}

I want to count the most common Tags applied to Articles. How do I do this in LINQ?

I tried the below code which only ordered the Tags:

  var tagsFromDb = db.Tags
                  .GroupBy(q => q.UrlSlug)
                  .OrderByDescending(gp => gp.Count())
                  .Select(g => g.FirstOrDefault());
  • 1 1 Answer
  • 0 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. Editorial Team
    Editorial Team
    2026-05-25T20:13:06+00:00Added an answer on May 25, 2026 at 8:13 pm

    If I understand right you have a many-to-many relationship between articles and tags but the navigation property on the tag side is not exposed (there is no ICollection<Article> in Tag). I think in this case you have to start from the articles to get all used tags along with the information how often they are used in the articles:

    var tagQuery = db.Articles
        .SelectMany(a => a.Tags)          // flat enumeration of all used tags
        .GroupBy(t => t, (k, g) => new    // k = key = Tag, g = group of same tags
        {
            Tag = k,                      // the tag
            Count = g.Count()             // how often does it occur in the articles
        })
        .OrderByDescending(g => g.Count); // most used tags first
    

    If you want all tags sorted descending, call

    var tagList = tagQuery.ToList();
    

    If you just want the tag which is most often used in the articles, call

    var mostUsedTag = tagQuery.FirstOrDefault();
    

    In both cases the result is a collection/single object of an anonymous type which has the Tag and Count as members. If you just want the tag(s) and are not interested in the Count anymore you can project before apply ToList or FirstOrDefault: tagQuery.Select(anonymous => anonymous.Tag).....

    Edit

    Just saw the Edit in your question. Now, when you have an Article collection in the Tag entity it is easier:

    var tagQuery = db.Tags
        .Select(t => new
        {
            Tag = t,                      // the Tag
            Count = t.Articles.Count()    // just count the number of artices
                                          // the tag is used in
        })
        .OrderByDescending(x => x.Count); // most used tags first
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this classes who is mapped using Entity Framework Code First: public class
Using the Entity Framework Code First paradigm I have defined the following objects and
I have been using Entity Framework CTP with Code-First as in this tutorial by
I'm having trouble mapping two entities together with Entity Framework CTP5 using Code First
Using the Entity Framework 4. I have created a Code First database in the
I'm using Entity Framework 4.1 Code First and have been trying to add a
I am using Entity Framework code first and ASP.NET MVC 3. I have the
I have a project using Entity Framework code first. For various reasons one of
I am using Entity Framework 4.1 (code first) and have defined entities similar to
Is there a way in Entity Framework 4 (using CTP4 and Code First if

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.