2012-09-05 60 views
0

我試圖序列場地/欣欣關係在LINQ to Javascript和有很難搞清楚它們分組,基本上我已經從LINQ序列化LINQ到實體,以JSON

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 

以下輸出眼下查詢我是這樣的:

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); 

但是我希望做的是,把LINQ的地點名稱是這樣的:

{[ 
"VenueID" : 74, 
"VenueName" : "CitySars Cinema", 
"VenueShowtimes" : [ {"Late Night", 2}, {"Sunrise" , 3}, etc... ] 
]} 

我該怎麼做?

回答

1

大廈關閉的Leniel的代碼,這裏是使用你的JavaScriptSerializer,並應與你的SELECT語句工作的一個版本:

var venuesGrouped = enVenues.GroupBy(v => v.VenueName) 
     .Select(group => 
      new 
      { 
       VenueId = group.First().VenueId, 
       VenueName = group.Key, 
       VenueShowTimes = group.Select(v => new { PartyName = v.PartyName, PartyId = v.PartyId }) 
      }); 

string jsonresult = rSerialize.Serialize(venuesGrouped); 
3

這是(這是一個很好的鍛鍊):

public class Venue 
{ 
    public int VenueId {get; set;} 
    public string VenueName {get; set;} 
    public string PartyName {get; set;} 
    public int PartyId {get; set;} 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     List<Venue> venues = new List<Venue>() 
     { 
      new Venue() { VenueId = 74, 
          VenueName = "CityStars Cinema", 
          PartyName = "Late Night (3am)", 
          PartyId = 2}, 

      new Venue() { VenueId = 74, 
          VenueName = "CityStars Cinema", 
          PartyName = "Sunrise (6am)", 
          PartyId = 3}, 

      new Venue() { VenueId = 74, 
          VenueName = "CityStars Cinema", 
          PartyName = "Morning (9am)", 
          PartyId = 4}, 

      new Venue() { VenueId = 74, 
          VenueName = "CityStars Cinema", 
          PartyName = "Noon (12pm)", 
          PartyId = 5}, 

      new Venue() { VenueId = 74, 
          VenueName = "CityStars Cinema", 
          PartyName = "After Noon (3pm)", 
          PartyId = 6}, 
     }; 

     var venuesGrouped = venues.GroupBy(v => v.VenueName). 
        Select(group => 
         new 
         { 
          VenueId = group.First().VenueId, 
          VenueName = group.Key, 
          VenueShowTimes = "[" + group. 
       Select(v => string.Format(@"{{{0}, {1}}}", v.PartyName, v.PartyId)). 
       Aggregate((party1, party2) => party1 + ", " + party2) + "]" 
         }); 

     foreach (var venue in venuesGrouped) 
     { 
      Console.WriteLine(
      string.Format("{{[\"VenueID\" : {0},\n\"VenueName\" : {1}\n,\"VenueShowTimes\": {2}]}}", 
      venue.VenueId, venue.VenueName, venue.VenueShowTimes)); 
     } 


    } 
}