0

linqのVenue/ShowtimeリレーションをJavascriptにシリアル化しようとしていますが、それらをグループ化するのに苦労しています。基本的に、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

今私が持っているクエリはこれです:

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... ]
]}

これどうやってするの?

4

2 に答える 2

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


    }
}
于 2012-09-05T16:11:34.000 に答える
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);
于 2012-09-05T18:11:06.000 に答える