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 回答