I’m trying to serialize a Venue/Showtime relation in linq to Javascript and having hard time figuring out to group them, basically I have the following output from the linq
Venue ID | Venue Name | Party Name | Party ID
74 CityStars Cinema Late Night (3am) 2
74 CityStars Cinema Sunrise (6am) 3
74 CityStars Cinema Morning (9am) 4
74 CityStars Cinema Noon (12pm) 5
74 CityStars Cinema After Noon (3pm) 6
Right now the query I have is this:
JavaScriptSerializer rSerialize = new JavaScriptSerializer();
var enVenues = from v in db.Venues
join t in db.VenueTimes on v.ID equals t.VenueID
join p in db.VenueParty on t.PartyID equals p.ID
select new
{
VenueID = v.ID,
VenueName = v.TitleEn,
PartyName = p.NameEn,
PartyID = p.ID
};
rMovie.VenuesArray = rSerialize.Serialize(enVenues);
But What I would like to do is to group the linq by Venue Name to something like this:
{[
"VenueID" : 74,
"VenueName" : "CitySars Cinema",
"VenueShowtimes" : [ {"Late Night", 2}, {"Sunrise" , 3}, etc... ]
]}
How can I do this?
Building off of Leniel’s code, here is a version that uses your
JavaScriptSerializerand should work with your select statement: