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?
Counts by tag:
Most used tag:
50 most used tags (may not actually have 50 there):
Freehand, so might not be 100% syntactically correct or the smallest/most readable way to do it.
Edit: Added Example: