Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert datatable to private List<List<string>>?

Any fast way to convert a datatable to List<List<string>> ?

right now i am doing

for (int rowIndex = 1; rowIndex <= stats.EndRowIndex; rowIndex++)
{
    List<string> lstOneRowElements = new List<string>();
    for (int colIndex = 1; colIndex <= stats.EndColumnIndex; colIndex++)
    {                                
        lstOneRowElements.Add(excelDoc.GetCellValueAsString(rowIndex, colIndex).Trim());
    }

    lstAllData.Add(lstOneRowElements);
}

where

private List<List<string>> lstAllData { get; set; }

any better way to do it fast ?

like image 841
Rahul Chowdhury Avatar asked Sep 09 '26 07:09

Rahul Chowdhury


1 Answers

If you have a DataTable (see comment Mithon) then you can try this

var q = from row in dt.AsEnumerable()
        select row.ItemArray.Select(x => x.ToString()).ToList<string>();

var y = q.ToList();
like image 83
Hans Derks Avatar answered Sep 10 '26 22:09

Hans Derks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!