Alex
Alex

Reputation: 8937

Combine several DataTables DataRows

I have a Dataset of DataTables with same structure. I want to get a single collection of DataRows from these tables in SINGLE linq query

from sourceTab in ds.Tables.OfType<DataTable>() 
select sourceTab

Then I need to select datarows from each table and combine them into one list

Upvotes: 3

Views: 578

Answers (3)

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98810

Try with Cast<T> and SelectMany<T>

var r = ds.Tables.Cast<DataTable>().SelectMany(n => n.AsEnumerable())

Upvotes: 2

MarcinJuraszek
MarcinJuraszek

Reputation: 125640

You don' need query syntax to do that. Simple method query is fine:

var results = ds.Tables.Cast<DataTable>().SelectMany(t => t.AsEnumerable())

Upvotes: 2

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236268

Use SelectMany to select all rows from each table and flatten them into one sequence:

from row in ds.Tables.Cast<DataTable>().SelectMany(t => t.AsEnumerable())
// filter, etc 
select row

If you just want all rows, then use

ds.Tables.Cast<DataTable>().SelectMany(t => t.AsEnumerable())

Upvotes: 2

Related Questions