Question Datarow and List(of T)

demi_rapidshare

New member
Joined
May 25, 2010
Messages
1
Programming Experience
Beginner
I'd like to know a few things:
1. How can I populate a List(of Integer) from datarow
2. How can I take only certain columns from datatable.select() and copy the data to datarow? So it's like this:
datatable dt has columns ID,Name,age,address and DOB
I want to take only the columns name, address and age where age>40 and put the result set into a datarow dr.
 
What's the relationship between the List and the DataRow? Are we talking about all the values from one column in a DataTable? If so:
VB.NET:
Dim numbers As New List(Of Integer)

For Each row As DataRow In myDataTable.Rows
    numbers.Add(CInt(row("Number")))
Next
If it's something else you want then you'll have to explain.

2. I am assuming that you want to create a duplicate DataTable but with fewer columns and the data filtered. In that case, don't use DataTable.Select; there's a better way:
VB.NET:
Dim view As New DataView(table1)

view.RowFilter = "Age > 40"

Dim table2 As DataTable = view.ToTable(False, "Name", "Address", "Age")
That said, depending on what you want to do with the data, it might not even be necessary to do that. If you'd care to explain a little further...
 
Actually the first and second issue are related. What I really want is taking certain column from existing datatable (filtered result set) and put the result into List(Of Integer).

Yes, I know if you use add one by one, all is solved. What I'd like to know if there's an "elegant" way to populate List(of Integer) like:
dim l as new List(Of Integer)(dr1)
 
As you're using .NET 3.5, there is a more elegant way: LINQ. E.g.
VB.NET:
Dim numbers = (From row In myDataTable.AsEnumerable() _
               Where row.Field(Of Integer)("Age") > 40 _
               Select row.Field(Of Integer)("ID")).ToList()
 
What I've provided is all the code you need. 'numbers' is a List(Of Integer). The ToList method returns a List whose generic type is whatever the Select clause of the query returns. In this case the Select clause returns:
VB.NET:
row.Field(Of Integer)("ID")
which is obviously an Integer, so ToList returns a List(Of Integer). If the Select clause was:
VB.NET:
row.Field(Of String)("Name")
then ToList would return a List(Of String).

I haven't declared the type of 'numbers' because it is inferred, but you can be explicit if you want:
VB.NET:
Dim numbers As List(Of Integer) = ...
 
VB.NET:
public Class MyClass

   dim MyList as List(Of Integer)

   private sub FillList(byval tList as List(Of Integer))
      MyList=tList
   end Sub

   Private sub MyClass_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      dim n as new List(Of Integer)
      n=(From row In dt.AsEnumerable() Where row.Field(Of Integer)("class_room_id") Mod 2 <> 0 Select row.Field(Of Integer)("class_room_id")).ToList()
   End Sub

End Class

got an error. What did I do wrong?
 
Um, how many people are posting to this thread? You sound like you're the OP yet you have a different user name.
got an error. What did I do wrong?
What you did wrong was tell us that you got an error without telling us what the error message was and exactly where it occurred.

Also, as I said earlier, the ToList method returns a List. As such, there's no point in your creating a new List and then immediately discarding it to use a different one. Don't create a new List unless you need a new List, which you don't because ToList is returning one.
 
Ah, sorry. Silly mistake. I havent' instantiate the object. The error was null reference something something. thanks anyway for the hlp.
 
Back
Top