By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

Dataset to Generic.List(Of MyClass)

P: n/a
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

Sep 19 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a

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

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Wed, 19 Sep 2007 16:36:02 -0700, shapper <md*****@gmail.comwrote:
>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
Sep 20 '07 #2

P: n/a
You could use custom attributes to handle this dynamically.
Check out what this article does.

http://www.eggheadcafe.com/articles/..._generator.asp

--
Robbe Morris [Microsoft MVP - Visual C#]
..NET PropertyGrid Control - ListBox, ComboBox, and Custom Classes
http://www.eggheadcafe.com/tutorials...d-control.aspx


"shapper" <md*****@gmail.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...
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
Sep 20 '07 #3

P: n/a
Havent tried it, but your code looks to be spot on.

"Samuel R. Neff" <sa********@nomail.comwrote in message
news:41********************************@4ax.com...
>
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

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Wed, 19 Sep 2007 16:36:02 -0700, shapper <md*****@gmail.comwrote:
>>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

Sep 20 '07 #4

P: n/a
On Wed, 19 Sep 2007 16:36:02 -0700, shapper <md*****@gmail.comwrote:
>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<CountryGetCountries_FromDataSource()
{
List<Countrycountries = 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;
}
Sep 21 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.