How do I take a DataTable and convert it to a List?
I\'ve included some code below in both C# and VB.NET, the issue with both of these is that we create a new object
Why not pass the DataTable into to the function instead of instantiating it? That would simply contain a reference.
That's way too simple an answer too be worthwhile to you I'm sure, but I don't see how it doesn't solve your problem.
This is one simple way to do it. Say you have a history of events to pull from the database and you've a class like this:
public class EventHistRecord
{
public string EvtDate { get; set; }
public string EvtType { get; set; }
public string EvtUser { get; set; }
public string EvtData { get; set; }
}
Then from database you pull event history records in a dataset:
DataSet ds = SqlQueries.getDataSet("select * from EventHist", phoneNum);
And then from the dataset in to a List of EventHistRecord:
List<EventHistoryRecord> lstRecs = ds.Tables[0].AsEnumerable().Select(
dr => new EventHistoryRecord { EvtDate= dr["EvtDate"].ToString(),
EvtData = dr["EvtData"].ToString(),
EvtType = dr["EvtType"].ToString(),
EvtUser = dr["EvtUser"].ToString()
}).ToList();
I have another approach that might be worth taking a look at. It's a helper method. Create a custom class file named CollectionHelper:
public static IList<T> ConvertTo<T>(DataTable table)
{
if (table == null)
return null;
List<DataRow> rows = new List<DataRow>();
foreach (DataRow row in table.Rows)
rows.Add(row);
return ConvertTo<T>(rows);
}
Imagine you want to get a list of customers. Now you'll have the following caller:
List<Customer> myList = (List<Customer>)CollectionHelper.ConvertTo<Customer>(table);
The attributes you have in your DataTable must match your Customer class (fields like Name, Address, Telephone).
I hope it helps!
For who are willing to know why to use lists instead of DataTables: link text
The full sample:
http://lozanotek.com/blog/archive/2007/05/09/Converting_Custom_Collections_To_and_From_DataTable.aspx
If the properties of the list match the field names in the datatable you should be able to create some kind of generic reflection routine.
No, creating a list is not costly. Compared to creating the data table and fetching the data from the database, it's very cheap.
You can make it even cheaper by creating the list after populating the table, so that you can set the initial capacity to the number of rows that you will put in it:
IList<INote> notes = new List<INote>(table.Rows.Count);
No sure if this is what your looking for but you could try something like this.
public class Category
{
private int _Id;
public int Id
{
get { return _Id; }
set { _Id = value; }
}
private string _Name = null;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public Category()
{}
public static List<Category> GetCategories()
{
List<Category> currentCategories = new List<Category>();
DbCommand comm = GenericDataAccess.CreateTextCommand();
comm.CommandText = "SELECT Id, Name FROM Categories Order By Name";
DataTable table = GenericDataAccess.ExecuteSelectCommand(comm);
foreach (DataRow row in table.Rows)
{
Category cat = new Category();
cat.Id = int.Parse(row["Id"].ToString());
cat.Name = row["Name"].ToString();
currentCategories.Add(cat);
}
return currentCategories;
}
}
This is what I have done so hope this helps. Not sure if it is the right way to do it but it works for what we needed it for.