Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert DataSet to List

Here is my c# code

Employee objEmp = new Employee(); List<Employee> empList = new List<Employee>(); foreach (DataRow dr in ds.Tables[0].Rows) {     empList.Add(new Employee { Name = Convert.ToString(dr["Name"]), Age = Convert.ToInt32(dr["Age"]) }); } 

It uses a loop to create a List from a dataset.Is there any direct method or shorter method or one line code to convert dataset to list

like image 448
iJade Avatar asked Jun 14 '13 11:06

iJade


People also ask

Can we convert DataTable to List?

There are the following 3 ways to convert a DataTable to a List. Using a Loop. Using LINQ. Using a Generic Method.

How do you convert a DataSet to a List in Python?

To convert Pandas DataFrame to List in Python, use the DataFrame. values(). tolist() function.


1 Answers

Try something like this:

var empList = ds.Tables[0].AsEnumerable()     .Select(dataRow => new Employee     {         Name = dataRow.Field<string>("Name")     }).ToList(); 
like image 190
Carra Avatar answered Oct 05 '22 14:10

Carra