ef code first - Linq Query With Entities and Group By -
i have entities shown below (using .net code first repository pattern):
public abstract class basecontent : entity { public guid id { get; set; } public virtual icollection<assignedmedia> assignedmedias { get; set; } } public partial class library : basecontent { public library() { this.assignedmedias = new list<assignedmedia>(); } } public class media : basecontent { public media() { this.assignedmedias = new list<assignedmedia>(); } } public class assignedmedia : entity { public int id { get; set; } //basecontent reference id public guid assignedid { get; set; } public guid mediaid { get; set; } public virtual media media { get; set; } public virtual basecontent basecontent { get; set; } }
then i'm trying query (as dictionary<string, list<media>>) group by! instance:
var list = bc in libraries join in assignedmedias on bc.id equals a.assignedid join m in medias on a.mediaid equals m.id select new { libraryname = bc.name, medialist = ??? };
you don't need use join
when have entity associations mapped properly. can write query so:
var libraryassignedmedia = libraries.todictionary( l => l.name, // key selection. l => l.assignedmedias.select(am => am.media)); // value selection.
Comments
Post a Comment