JohnB
JohnB

Reputation: 19012

LINQ to DataSet to get generic list from DataTable

DataTable table = DataProvider.GetTable()

var clientIds = from r in table.AsEnumerable()
                select r.Field<string>("CLIENT_ID");

I want clientIds to be a List<string>. Currently it's an EnumerableRowCollection<>

What am I missing?

Upvotes: 0

Views: 1862

Answers (2)

RQDQ
RQDQ

Reputation: 15579

Here is one way to do it:

var clientIds = table.Rows.Cast<DataRow>().Select(r => r.Field<string>("CLIENT_ID").ToList();

Or, if this syntax is working but not bringing back the results as a list, you can do something like:

var clientIds = (from r in table.AsEnumerable()
                select r.Field<string>("CLIENT_ID")).ToList();

Upvotes: 1

stack72
stack72

Reputation: 8288

this may work

DataTable table = DataProvider.GetTable()

var clientIds = (from r in table.AsEnumerable()
                select r.Field<string>("CLIENT_ID")).ToList();

Upvotes: 3

Related Questions