tags:

views:

153

answers:

3

In ASP.NET 3.5 / Silverlight, I have a situation where I need to send four small Lists from the server to the client. Each sublist is a list of objects. I would like to do this with one call from the client.

So, for example:

ListA is a List of POHeader objects ListB is a List of POLine objects ListC is a List of Vendor objects ListD is a List of Project objects

ListX would be a List containing each of the four lists.

Each of these objects has a different structure. When the List gets back to the client, I'll take it apart and bind each of the four sublists to the relevant control.

Is this possible in C#. I've seen examples of lists of list, but each of the sublists was the same type.

Many thanks Mike Thomas

+4  A: 

Since you always have 4 lists, just make a custom class to hold them, and pass that back:

class POCollection
{
     IList<POHeader> Headers { get; private set; }
     IList<POLine> Lines { get; private set; }
     // etc...
}
Reed Copsey
Any time you start having lists of lists of lists you should start trying to see if you actually have a definable object.
DataDink
DataDink - could you elaborate on this a little bit? Could you provide an example??Mike Thomas
A: 

You could send them back as a list of objects and then use the OfType query operator to reconstruct the lists on the client:

List<object> allItems;
var headers allItems.OfType<POHeader>().ToList();
var lines = allItems.OfType<POLine>().ToList();
...

Although I'd make a DTO with each list as a member which is more helpful to any callers.

Lee
Many thanks Reed - I'll take a look at the DTO approach.
A: 

You could also keep them in separate lists cast back to objects... and then recast them at the destination.

List<POHeader> ListA;
List<POLine> ListB;
List<Vendor> ListC;
List<Project> ListD;
...
List<List<object>> message = new List<List<object>>(){
ListA.Cast<object>().ToList(),
ListB.Cast<object>().ToList(),
ListC.Cast<object>().ToList(),
ListD.Cast<object>().ToList()
};

And then cast back on retrieval...

List<POHeader> ListA = message[0].Cast<POHeader>().ToList();
...
tbischel