C# Datatable to IList conversion -


i using below code convert dataset ilist. there better way write code below:

ilist lst = new list<object>(); var tbl = dslist.tables[0]; foreach (datarow dr in tbl.rows) {    lst.add(dr.itemarray); } 

using system.linq;       ilist<object> lst = dslist.tables[0].rows.oftype<datarow>().select(x => x.itemarray).tolist(); 

simply using system.link's select() method on rows collection of table. extension method can called on ienumerable<t> (means on object implements interface directly or through inheritance).

if don't have ienumerable<t> query, rather simple ienumerable (non generic), can use trick of calling .oftype() on it, returns generic instance.

if want flatten out enumerable, can use selectmany().

if you're stuck on pre-linq .net, must have loop.


Comments

Popular posts from this blog

java - HTTP Status 500 - An exception occurred processing JSP page /second.jsp at line 15 -

php - Using str_replace to translate a MySQL Table in html -

SQL Server - MyCTE query based on 24 hour period (next day) -