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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:01:42+00:00 2026-05-28T00:01:42+00:00

My setup I have two classes, here shown stripped down to what is needed

  • 0

My setup

I have two classes, here shown stripped down to what is needed in this example:

public class Photo : Entity
{
    [Key]
    public int Id { get; set; }

    public virtual ICollection<Tag> Tags { get; set; }

    public Photo()
    {
        Tags = new List<Tag>();
    }
}


public class Tag : Entity
{
    [Key]
    public string Text { get; set; }

    public virtual ICollection<Photo> Photos { get; set; }

    public Tag()
    {
        Photos = new List<Photo>();
    }
}

As shown above theres is a many-to-many relationship between the two entities.

I’m using EF 4.1 code-first.

Example:

  • "photo1" has "tag1", "tag2" and "tag3" in its tags navigation property.
  • "photo2" has "tag2", "tag3" and "tag4" in its tags navigation property.
  • "photo3" has "tag2" and "tag4" in its tags navigation property.

Total tag count in all photos:

  • "tag1": 1
  • "tag2": 3
  • "tag3": 2
  • "tag4": 2
  • Total tags: 8

Note

My end goal is this tag cloud, but using MVC3:

http://www.geekzilla.co.uk/View960C74AE-D01B-428E-BCF3-E57B85D5A308.htm

First question

How do I (using EF) find out how many times the most used tag(s) is used (finding the count of "tag2")?
And the same for the least used tag(s) (count of "tag1" in above example).

In the link above these to lines of code is used:

double.TryParse(tagData.Compute("min(count)", null).ToString(), out min);
double.TryParse(tagData.Compute("max(count)", null).ToString(), out max);

What is the EF/LINQ equivalent?

Second question

How do I get the count for each tag or the count for the 50 most used tags?

  • 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-28T00:01:43+00:00Added an answer on May 28, 2026 at 12:01 am

    Counts by tag:

    from t in Context.Tags
    select new
    {
        t.Text,
        t.Photos.Count()
    }
    

    Most used tag:

    (from t in Context.Tags
    let photoCount = t.Photos.Count()
    orderby photoCount descending
    select new
    {
        t.Text,
        photoCount
    }).FirstOrDefault()
    

    50 most used tags (may not actually have 50 there):

    (from t in Context.Tags
    let photoCount = t.Photos.Count()
    orderby photoCount descending
    select new
    {
        t.Text,
        photoCount
    }).Take(50)
    

    Freehand, so might not be 100% syntactically correct or the smallest/most readable way to do it.

    Edit: Added Example:

    foreach(var result in Context.Tags.Select(x => new { t.Text, t.Photos.Count() }))
    {
        Console.WriteLine(string.Format("Text: {0}, Count: {1}", result.Text, result.Count.ToString());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Setup Using MVC 3 + Code First Here are my classes public class Member
I have a two models set up like this: class User < ActiveRecord::Base #
First, a little background (greatly simplified): I have two classes, one called Entity and
I have a very small entity framework setup containing only a few related classes/tables
I have two tables, Locations and Facilities They map to two classes, public Location
I have two issues both related to (I believe) my SQL Server setup. I
I have a Project Collection setup in my TFS2010RC deployment. I have two Projects
I need to setup LookAndFeel Files in JDK 1.6. I have two files: napkinlaf-swingset2.jar
HELP! I just setup a virtual host for two sites that have a lot
I have two Spring proxies set up: <bean id=simpleBean class=org.springframework.aop.framework.ProxyFactoryBean> <property name=target> <ref local=simpleBeanTarget/>

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.