Dataset to Generic.List(Of MyClass)

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I am getting various records from a database:

Dim ds As DataSet = db.ExecuteDataSet(dbc)

Each record has 3 fields: Id, Name and Text.

I have a class named MyClass with 3 properties Id, Name and Text.

I need to get the values of my dataset and fill a Generic.List(Of
MyClass)

How can I do this?

Thanks,

Miguel
 
If you have only the one class that you're using (MyClass) then create
a manual procedure to loop through the data and create a list, it'll
be the fastest.

Dim table As DataTable = ds.Tables(0)
Dim id As DataColumn = table.Columns("Id")
Dim name As DataColumn = table.Columns("Name")
Dim text As DataColumn = table.Columns("Text")
Dim list As New List(Of MyClass)
Dim obj As MyClass

For Each row As DataTable in table.Rows
obj = New MyClass()
obj.Id = row(id)
obj.Name = row(name)
obj.Text = row(text)
Next

My VB.NET is a bit rusty so it may not compile, but you get the idea.
Note caching of DataColumn references and using them to lookup
values--that's the fastest access mechansim for data in a row.

If you have lots of classes that you need to create then you can look
at alternatives. My suggestion would be code generation at compile
time to create converter classes. This would provide the best runtime
performance.

Another option would be reflection, but it would provide the poorest
runtime performance and in this situation probably should only be used
if performance is not a consideration.

Also if your goal is to just create List(Of MyClass) from the database
records then you're better off using a data reader instead of a data
set, it'll provide better performance. The data set is a disconnected
storage mechanism that also provides a lot of other features, but if
all you're doing is reading data and generating lists of classes,
there's no need for the dataset features so a data reader is better in
this situation.

HTH,

Sam
 
Hello,

I am getting various records from a database:

Dim ds As DataSet = db.ExecuteDataSet(dbc)

Each record has 3 fields: Id, Name and Text.

I have a class named MyClass with 3 properties Id, Name and Text.

I need to get the values of my dataset and fill a Generic.List(Of
MyClass)

How can I do this?

Thanks,

Miguel

That is a very slow way of loading a Generic list. You will notice just
how slow it is if your DataSet gets too big. The best way is to use a
datareader and to load the generic list as from the reader.

Here is an example in C#

public static List<Country> GetCountries_FromDataSource()
{
List<Country> countries = new List<Country>();
using (SqlConnection dConn = new
SqlConnection(data_connection.SqlConnectionString))
{
using (SqlCommand cmd = new SqlCommand("Country_Get_All", dConn))
{
cmd.CommandType = CommandType.StoredProcedure;
try
{
dConn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Country country = new Country
(
(int)reader["CountryID"],
reader["CountryName"].ToString(),
(int)reader["Division"]
);
countries.Add(country);
}
}
}
catch (Exception e)
{ countries = null; }
}
}
return countries;
}
 
Back
Top