score:3
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
Source: stackoverflow.com
Related Articles
- Query a many to many relationship with Entity Framework and .NET 3.5
- many to many mapping in entity framework code first
- updating data in many-to-many relationship in entity framework in code first existing database
- Counting in a Many-To-Many Relationship in Entity Framework
- Entity Framework code first - Many to Many - Include conditional
- Counting occurrences in a many to many table with Entity framework and LINQ
- linq to one to many relationship Entity framework
- how to select data by linq in many-to-many relationship in First code Entity framework 5
- Why Entity Framework Code First one to many Doesn't work properly
- To obtain a result list from many to many relationship in Entity Framework
- How to configure one to many relationship using Entity Framework Core fluent API
- Entity Framework Code First Select Item Based on Relationship
- one to many relationship in Entity Framework on the same table
- How to join columns or properties of entities that belongs as many relationship to another entity in .NET EF Code First
- Proper Linq Query for objects with many to many relation ship generated with code first entity framework
- Entity Framework One to Many Relationship Model - How to select column from the child table
- Filtering Data in Many To Many Relationship in Entity Framework
- Entity Framework Core: many-to-many relationship with same entity
- Entity Framework 6 Code First Custom Functions
- Entity Framework - querying a many-to-many relationship table
- Entity Framework - An item with the same key has already been added. - Error when trying to define foreign key relationship
- Avoiding repeated projection code in Entity Framework
- Forcing Entity Framework to not generate NCLOB's when building Linq-to-Sql Code (Model First)
- Entity Framework Code First without app.config
- How to query many-to-many relationship with 'AND' condition using LINQ and Entity Framework
- Force inner join with many-to-many relationship entity framework
- Entity Framework Many to Many works but Include does not
- Entity Framework Code First using context in Controller
- Entity Framework Many to Many query
- How to Create a Run-Time Computed (NotMapped) Value in Entity Framework Code First
- Lazy combinations
- LINQ Group and Count with a Max
- Setting a primary key with ROW_NUMBER in a view mapped with Entity Fluent API makes linq timeout
- Identify methodinfo that has HTTPPost() attribute
- LINQ ToDictionary System.InvalidCastException: 'Unable to cast object of type 'System.Int32' to type 'System.String'.'
- How to solve the cast to value type 'System.Decimal' failure error
- Linq to SQL to aggregate data by week
- Applying a predicate to a joined table
- Query multiple tables with LINQ, grab results to dictionary
- Get distinct Combinations of numbers using LINQ
- How to pass parameter to Linq Orderby
- Linq to Entities group query giving list of results in each group
- read xml attributes
- LINQ query to ado.data entity model selecting more than one column in c#
- How do I use Linq ToDictionary to return a dictionary with multiple values in the dictionary items?
- Linq selecting object from relational list of object by search keyword
- Is there a LINQ-oriented method to quicky evaluate nested lists in dictionaries
- Moving specific elements after others but not to top or bottom
- EF does not recognize the method 'Boolean Contains(System.Object)' method, and this method cannot be translated into a store expression
- LINQ. Request for understanding multiple select and from and where in C#