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
There are the following 3 ways to convert a DataTable to a List. Using a Loop. Using LINQ. Using a Generic Method.
To convert Pandas DataFrame to List in Python, use the DataFrame. values(). tolist() function.
Try something like this:
var empList = ds.Tables[0].AsEnumerable() .Select(dataRow => new Employee { Name = dataRow.Field<string>("Name") }).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With