1

I have a web method in a c# web service which creates three lists, which are filled from xml input. I want to combine these three lists into one entity (a DataSet would be the best, as the iOS app that is consuming this web service is already programmed to accept and parse DataSets), and return them from the web method.

Here is currently what my code looks like:

[WebMethod]
public DataSet SelectObjects(string ExternalID, string Password)
{
     DataSet ds = new DataSet();

     MembershipAuthServiceReference.MembershipAuthenticationService objService = new MembershipAuthServiceReference.MembershipAuthenticationService();
     MembershipAuthServiceReference.SoapHeaderCredentials objSoapHeader = new MembershipAuthServiceReference.SoapHeaderCredentials();
     MembershipAuthServiceReference.MemberUserInfo objMemberInfo = new MembershipAuthServiceReference.MemberUserInfo();

     try
     {
         objSoapHeader.UserName = ExternalID;
         objSoapHeader.Password = Password;

         objMemberInfo = objService.GetMembershipInfo();

         List<Obj1> ListObj1 = new List<Obj1>();

         for (int i = 0; i < objMemberInfo.Obj1.Length; i++)
         {
             Obj1 obj_Obj1 = new Obj1();
             obj_Obj1.Stuff = objMemberInfo.Obj1[i].Stuff.ToString();
             ListObj1.Add(obj_Obj1);
         }

         List<Obj2> ListObj2 = new List<Obj2>();

         for (int i = 0; i < objMemberInfo.Obj2.Length; i++)
         {
             Obj2 obj_Obj2 = new Obj2();
             obj_Obj2.Stuff = objMemberInfo.Obj2[i].Stuff.ToString();
             ListObj2.Add(obj_Obj2);
         }

         List<Obj3> ListObj3 = new List<Obj3>();

         for (int i = 0; i < objMemberInfo.Obj3.Length; i++)
         {
             Obj3 obj_Obj3 = new Obj3();
             obj_Obj3.Stuff = objMemberInfo.Obj3[i].Stuff.ToString();
             ListObj3.Add(obj_Obj3);
         }

     }
     catch (Exception ex)
     {
         string sError;
         sError = ex.Message.ToString();
     }
     return ds;

}

How do I combine these lists into a DataSet? I'm assuming it's possible? If not, is there a viable alternative that does the same thing?

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
optionsix
  • 956
  • 1
  • 8
  • 27
  • you can use Concat method to the concatenate the lists as in var combinedList= ListObj1.Concat(ListObj2).Concat(ListObj3); – Dan Hunex Aug 23 '13 at 16:51

1 Answers1

1

First concatenate your lists as shown below and then use the link to generate the dataset

 var combinedList= ListObj1.Concat(ListObj2).Concat(ListObj3); 

How do I transform a List<T> into a DataSet?

Community
  • 1
  • 1
Dan Hunex
  • 5,172
  • 2
  • 27
  • 38
  • instead of concat, you could just add everything to a single list to begin with. theres no reason in the code you showed to be creating 3 separate List objects – Robert Levy Aug 23 '13 at 17:12
  • I guess this even doesn't compile. Could we concat lists of different types like a `List` and a `List`? – King King Aug 23 '13 at 17:23
  • I can use one single list even if the structures of Obj1, Obj2, and Obj3 are completely different? – optionsix Aug 23 '13 at 17:44
  • @KingKing it is a list of objects – Dan Hunex Aug 23 '13 at 17:48
  • @DanHunex if so, you have to state it clearly, because as in the OP's code, they are of different types. – King King Aug 23 '13 at 17:49
  • 1
    @optionsix of course you can't. However the `link` given above is helpful, you have to create each `DataTable` for each one in `Obj1, Obj2, Obj2` and add all the DataTables to a `DataSet`. – King King Aug 23 '13 at 17:51
  • Ah, thats the missing element. Convert the lists to DataTables, add the DataTables to a unified DataSet. That's what I wasn't doing. Worked like a charm, thanks! – optionsix Aug 23 '13 at 18:06