views:

92

answers:

1

Hi

I am trying to use the jquery DataTables plugin for my system, using a webservice to populate the data through an ajax call.

What I need to return from the webservice is a List like this: List<List<string>>;

I have about 120 different domain classes being used by this as the core data objects, so I need some kind of system where I can get the data from the different domain classes and transform them into List<List<string>> in a somewhat automatic way.

The way I am getting the data is something like this (example code):

List<Core.Room> coreDataList = Core.GetRoomsInHotel(int hotelID);

I have been looking into DynamicLinq and System.Reflection, but have gotten nowhere :(


UPDATE:

What I need to return from the webservice is an array like this:

[
    ["data-col-1","data-col-2","data-col-3","data-col-4"],
    ["data-col-1","data-col-2","data-col-3","data-col-4"],
    ["data-col-1","data-col-2","data-col-3","data-col-4"]
]

To populate this array, I have about 120 different lists of domain classes with different properties, and I need to access those properties for each one dynamically:

For a room, based on an object looking like this:

public class Room {
    public int ID { get; set; }
    public string Name { get; set; }
    public string Code { get; set; }
    public string LongName { get; set; }
}
[
    ["ID", "Double","DBL"],
    ["ID", "Single","SNG"],
    ["ID", "Suite","SUI"]
]

For a hotel:

public class Hotel {
    public int ID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public int RoomCount { get; set; }
    public int FloorCount { get; set; }
}
[
    ["1","Plaza","153","12"],
    ["2","Astoria","568","25"]
]

I have a XML file with config for the lists:

<list id="29">
  <readable_name>Hotels - Roomtypes - List all roomtypes in system</readable_name>
  <no_data_message>No roomtypes found</no_data_message>
  <cols>
    <col width="0" visible="false" datafield="ID" type="key" headertext="ID" />
    <col width="70" visible="true" datafield="Name" type="string" headertext="Name" />
    <col width="30" visible="true" datafield="Type" type="string" headertext="Type" />
  </cols>
</list>

So I pull the config from the XML file based on which list I want to load. I call the data store based on the list ID, and get a list of whatever domain class I want (Room/Hotel). I go through the returned list and produce kind of array so that the list on the page can be populated.

I hope this clarifies things a bit ...

+2  A: 
List<Core.Room> coreDataList = Core.GetRoomsInHotel(int hotelID); 
List<string> coreDataStrList = coreDataList
                                .Select(room => room.ToString())
                                .ToList();

Beyond that, we are going to need more information about your code.

UPDATE:

List<string> ToStringList<T>(this IEnumerable<T> list)
{
    list.Select(obj => obj.ToString()).ToList();
}

List<Core.Room> coreRoomList = ... 
List<Core.Hotel> coreHotelList = ... 

List<List<string>> ListofLists = new List<List<string>> ();

ListofLists.Add(coreRoomList.ToStringList());
ListofLists.Add(coreHotelList.ToStringList());

(It would kind-a help if you gave us more that one line of code at a time. Are these 120 lists grouped somehow? Do they have a common base?

James Curran
The thing is, I am looking for a dynamic way of doing this, as I said I have 120 lists like this and I don't want to do this selectively for all the lists.
Arni Gunnar
I updated the original question with some more code snippets ...
Arni Gunnar