开发者

Generate json result based on two related entites

I have two related one to many entities

Race and Cars (on race contains a lot of cars)

I need to generate an json result to pass it to jQGrid, i thought may be it is possible to do that without creating new class witch would contain properties. I thought I can go like that:

var jsonData = new
        {
            total = totalPages,
            page = page,
            records = totalRecords,

            rows = (from c in Races
                    select new
                    {
                        //c.Cars.Id.ToString(),  - need iteration
                        cell = new string[] {
                                 //c.Cars.Id.ToString(),  - need iteration
                                 c.Date.ToString(),
                                 c.Type.ToString(),
                                 c.Cars //But how i may loop all Cars colection here?
                                 //c.Cars.Name - need iteration
                                 //c.Cars.Speed - need iteration
                             }
                    }).ToArray()
        };

But the Cars property represent collection. How may i iterate that inside collection initializer? Or should i better create class witch would contain all the properties i need?

Any ideas?

Lets say Car has properties Name Speed Id and Race has properties Date, Type

The data will be displayed like开发者_JAVA百科 that:

Date       |  Type | Id  | Name      | Speed
02/03/2011 |   A   |  1  | MegaName1 |  130
02/03/2011 |   A   |  2  | MegaName2 |  112
02/03/2011 |   A   |  3  | MegaName3 |  132
03/05/2011 |   B   |  4  | MegaName2 |  112
03/05/2011 |   B   |  5  | MegaName4 |  33


Try the following:

var jsonData = new
{
    total = totalPages,
    page = page,
    records = totalRecords,

    rows = 
        (from race in races
         from car in race.Cars
         select new
         {
            cell = new string[] 
            {
                race.Date.ToString(),
                race.Type,
                car.Id,
                car.Name,
                car.Speed.ToString()
            }
         }).ToArray()
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜