C# How to convert a datarow["columnname"] of DataTable to List<string> which the datatype of the column is List<string> ?

Kay_Lee 101 Reputation points
2022-08-25T08:49:49.413+00:00

I have a DataTable with a column as green_dt.Columns.Add("overviews_list", typeof(List<string>));

foreach (DataRow dr in green_dt.Rows)  
                        {  
                            if (dr["overviews_list"] != DBNull.Value)  
                            {  
                                foreach (string family_overview in dr["overviews_list"]  * here is the problem)  
                                {  
  
                                }  
                            }  
                        }  

in the above code, * is problem.
The datatype of the column is LIst<string> but no relative methods are shown by Visual Studio like ToString() and so on. And Convert. Method also shows Int32, Int64 and so on but not ToList();

I've also failed to find some solution on internet.

This seems not pretty difficult but lost with busy real life...

Thank you !

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114.7K Reputation points
    2022-08-25T09:00:08.697+00:00

    Try this fix:

    foreach( string family_overview in (List<string>)dr["overviews_list"] )  
    {  
         
    }  
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful