将LINQ序列化为实体到JSON

时间:2012-09-05 15:37:21

标签: c# json linq entity-framework serialization

我正在尝试将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... ]
]}

我该怎么做?

2 个答案:

答案 0 :(得分: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));
        }


    }
}

答案 1 :(得分: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);