Datatable to Generic list

Posted by Emil under C# on 8/1/2013 | Points: 10 | Views : 1191 | Status : [Member] | Replies : 3
Want to convert datatable to generic list.

I don't want to use reflection technique. Want to convert to list using linq,

Using linq anyone had a idea how to convert datatable to generic list without using reflection technique




Responses

Posted by: Bandi on: 8/1/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer FromDataTableToList(DataTable dataTable) method in the following link
http://www.codeproject.com/Articles/33849/Tips-for-LINQ-Fill-any-generic-List-from-a-DataTab

By using LINQ
http://stackoverflow.com/questions/3982287/how-do-you-convert-a-dataset-datatable-into-a-generic-list-of-certain-type

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Emil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Emil on: 8/1/2013 [Member] Starter | Points: 25

Up
0
Down
I need a solution without using Reflection and generic

Emil, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 8/1/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You question is confusing.
First you said you want to convert data table to generic list and now you are saying that you dot want to use generic?
For generic list, you need to use generics.
Please clear you question before posting.

Solution using Generic List:-
1. You need to create a class file with properties matching with you table columns. E.g. MyType
2. Get the data from SQL and fill to Data-table. E.g. table
3. Fill the generic List with the Data-table values.
List<MyType> data = new List<MyType>(table.Rows.Count);
foreach (DataRow row in table.Rows)
{
data.Add(new MyType((int)row[0], (string)row[1])); //Create MyType class object and add the data to properties form table rows.
}
4. Your job is done.



Emil, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response