Sin5k4 Sin5k4 - 1 year ago 96
C# Question

One columned datatable to List<string>

I have a

which contains only one column and all items are strings. How can I convert this to a
using LINQ for example?

I Tried:

DataRow[] rows = dtusers.Select();
var qq = from RowCollection in rows
select new { UserCode = LibStatic.ToStr(RowCollection["UserCode"]) };

List<string> users = new List<string>();
users = qq.Cast<string>().ToList();

There is the easyway which always works:

foreach (DataRow dr in dtusers.Rows)

Answer Source

You can use LINQ query to do that.

List<string> list = dtusers.AsEnumerable()
                           .Select(r=> r.Field<string>("UserCode"))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download