How can convert a lit<> to a DataTable?
-
i have the code below and want to convert the results in
var rows
to a DataTablevar rows = new List<Row>();
var sr = new StreamReader(dirCSV + fileNevCSV);
while (!sr.EndOfStream)
{
string s = sr.ReadLine();
if (!String.IsNullOrEmpty(s.Trim()))
{
rows.Add(new Row(s));
}
}
sr.Close();"Coming soon"
-
i have the code below and want to convert the results in
var rows
to a DataTablevar rows = new List<Row>();
var sr = new StreamReader(dirCSV + fileNevCSV);
while (!sr.EndOfStream)
{
string s = sr.ReadLine();
if (!String.IsNullOrEmpty(s.Trim()))
{
rows.Add(new Row(s));
}
}
sr.Close();"Coming soon"
It would obviously depend on what sort of object is your Row class. Maybe your best bet would be to create the DataTable first and then add each DataRow while reading the csv files, instead of creating the List.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
-
i have the code below and want to convert the results in
var rows
to a DataTablevar rows = new List<Row>();
var sr = new StreamReader(dirCSV + fileNevCSV);
while (!sr.EndOfStream)
{
string s = sr.ReadLine();
if (!String.IsNullOrEmpty(s.Trim()))
{
rows.Add(new Row(s));
}
}
sr.Close();"Coming soon"
-
i have the code below and want to convert the results in
var rows
to a DataTablevar rows = new List<Row>();
var sr = new StreamReader(dirCSV + fileNevCSV);
while (!sr.EndOfStream)
{
string s = sr.ReadLine();
if (!String.IsNullOrEmpty(s.Trim()))
{
rows.Add(new Row(s));
}
}
sr.Close();"Coming soon"
Passing rows to the function below got it working.
public static DataTable ToDataTable(this IList data)
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
for(int i = 0 ; i < props.Count ; i++)
{
PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}"Coming soon"
-
i have the code below and want to convert the results in
var rows
to a DataTablevar rows = new List<Row>();
var sr = new StreamReader(dirCSV + fileNevCSV);
while (!sr.EndOfStream)
{
string s = sr.ReadLine();
if (!String.IsNullOrEmpty(s.Trim()))
{
rows.Add(new Row(s));
}
}
sr.Close();"Coming soon"
Best you would create a DataRow and add the content, then add it to IList collection. it will give a new extension method CopyToDataTable() which you can easily make conversion. IList iRows = new List(); DataTable iTable = iRows.CopyToDataTable();