group by - Linq-to-Entities: Ordered List aggregated from many-to-many relationship -


linq entities - many-to-many question:

i have songs table , tags table many-to-many table between. i'd count of songs each tag (a tally) , order tags starting least used. sql query looks this:

select name, count(name) timesused  tags inner join song_tag_junc stj on tags.id = stj.tagid group name order count(name) 

the entities song , tag this:

public class song {   public int id { get; set; }   public string name { get; set; }   public virtual icollection<tag> tags { get; set; } }  public class tag {   public int id { get; set; }   public string name { get; set; }   public virtual icollection<song> songs { get; set; } } 

any idea how accomplish in linq entities?

so think answered own question.

since tag entity references song entity, able write code below accomplish ordering tags least used greatest used:

var query = context.tags.orderby(tag => tag.songs.count());  list<tag> list = query.tolist(); 

there beauty in simplicity!


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -