Hi , I am creating in extension in Integration Studio with C#. That extension's C# method recieves a Record List data type from Outsystems Integration studio and needs to be converted to DataTable in C#. Any suggestions are much appreciated . Thanks in advace!
Hi Mahesh,
In C#, define the DataTable. Next, loop over the Record List. If it's a list of structures, you can use a foreach, where each element is an IRecord. If it's a list of entity records, you may need to use Reset, StartIteration, Eof, Current, Advance and EndIteration (accounts vary of whether this is strictly needed). So:
// normal structs foreach (IRecord r in recordList.Data) { // do something with r } // normal structs and entity records recordList.Reset(); recordList.StartIteration(); while (!recordList.Eof) { // do something with (IRecord)recordList.Current recordList.Advance(); } recordList.EndIteration();
Inside the foreach/while, you define a DataRow and assign it dt.NewRow(). Make sure you add the right columns with dr.Columns.Add(). Assign a value to the DataRow by indexing the right column and assigning.
Of course, if your input structure is generic, you need reflection to get the data out. If it's a structure defined in the extension, you can just use the attributes, which are prefixed with an "ss" prefix.
Hi Kilian Hekhuis, Thanks for your reply. I am trying with both ways using reflection and through IRecord. But its not working , datatable not creating with RLItemrecordList.
Request you please look at the below code snippet and suggest necessary changes required. Attached sample solution for reference. Thanks in advace!
public static DataTable RecordListToDataTable(RLItemRecordList _ssLineItems) { /* ---------------Option1-----------------------------*/ DataTable Dt = new DataTable(); DataTable dataTable = new DataTable(typeof(RLItemRecordList).Name); //Get all the properties PropertyInfo[] Props = typeof(RLItemRecordList).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in Props) { //Setting column names as Property names dataTable.Columns.Add(prop.Name); } foreach (RLItemRecordList item in _ssLineItems) { var values = new object[Props.Length]; for (int i = 0; i < Props.Length; i++) { //inserting property values to datatable rows values[i] = Props[i].GetValue(item, null); } dataTable.Rows.Add(values); } /* ---------------Option2-----------------------------*/ DataTable _dtItem = new DataTable(); foreach (IRecord r in _ssLineItems.Data) { DataRow _dr = _dtItem.NewRow(); _dr["InvoiceNumber"] = r. //values are populating with r } return Dt; }
For how to iterate over a generic record list, you could check the Forge assets XML Records, SortRecordList or Advanced Excel. The latter probably also uses a data table. I have an internal component similar to Advanced Excel that also uses a data table, but it's unfortunately too large to include here.