Converting DataTable to Generic List collection

Posted by Sheonarayan under LINQ category on | Points: 40 | Views : 1343
To convert DataTable to Generic List collection, use below approach

public IEnumerable<Person> LoadCollections()
DataTable table = LoadAll();
var list = (from DataRow row in table.Rows
select new Person
Active = bool.Parse(row["Active"].ToString()),
AutoId = int.Parse(row["Active"].ToString()),
FirstName = row["FirstName"].ToString(),
LastName = row["LastName"].ToString()
return list;

Here LoadAll() method is giving me data from database into table variable. Person is my object that should be returned in the list.

Comments or Responses

Login to post response