I am trying to Convert List to Dataset(Set of DataTables). But When I try this I got only 1 table. but according to my model It has to be 5 Tables. can anyone please advise me on this.
My Matrix Model
public class MatrixModel
{
public class RootObject
{
public string responseId { get; set; }
public string searchId { get; set; }
public int totalFound { get; set; }
public List<availableHotels> availableHotels { get; set; }
}
public class availableHotels
{
public string processId { get; set; }
public string hotelCode { get; set; }
public string availabilityStatus { get; set; }
public double totalPrice { get; set; }
public double totalTax { get; set; }
public double totalSalePrice { get; set; }
public string currency { get; set; }
public string boardType { get; set; }
public List<rooms> rooms { get; set; }
}
public class rooms
{
public string roomCategory { get; set; }
public List<paxes> paxes { get; set; }
public double totalRoomRate { get; set; }
public List<ratesPerNight> ratesPerNight { get; set; }
}
public class paxes
{
public string paxType { get; set; }
public int age { get; set; }
}
public class ratesPerNight
{
public string date { get; set; }
public double amount { get; set; }
}
}
List<MatrixModel.RootObject> ddd = JsonConvert.DeserializeObject<List<MatrixModel.RootObject>>("["+tstobj+"]");
Convert List to Dataset
DataSet dvv=ddd.ToDataSet<MatrixModel.RootObject>();
public static class ListConverter
{
/// <summary>
/// Convert our IList to a DataTable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns>DataTable</returns>
public static DataTable ToDataTable<T>(this IEnumerable<T> list)
{
Type elementType = typeof(T);
using (DataTable t = new DataTable())
{
PropertyInfo[] _props = elementType.GetProperties();
foreach (PropertyInfo propInfo in _props)
{
Type _pi = propInfo.PropertyType;
Type ColType = Nullable.GetUnderlyingType(_pi) ?? _pi;
t.Columns.Add(propInfo.Name, ColType);
}
foreach (T item in list)
{
DataRow row = t.NewRow();
foreach (PropertyInfo propInfo in _props)
{
row[propInfo.Name] = propInfo.GetValue(item, null) ?? DBNull.Value;
}
t.Rows.Add(row);
}
return t;
}
}
/// <summary>
/// Convert our IList to a DataSet
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns>DataSet</returns>
public static DataSet ToDataSet<T>(this IEnumerable<T> list)
{
using (DataSet ds = new DataSet())
{
ds.Tables.Add(list.ToDataTable());
return ds;
}
}
}
I got only One DataTable .
The main problem is that you want to convert hierarchical data to relational and that can not be done this easily. For example in your current hierarchical model you store a List
of availableHotels
for each RootObject
, but in a relational DB (like a DataSet
) you should store all the availableHotels
objects in a separate table then add a RootObjectId
column to this table and for each row set it to the id of the RootObject
object that is the parent of the availableHotels
stored in this row. And so on for the rest of your data.
This will need a totally different approach than the one you used in your code.
And of course you shouldn't put using
on the newly created objects if you want to return
and use them outside your method because it will dispose them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.