Visual Basic: Converting from DataTable to List

My DataTable has two columns (Integer, String)

And I need to convert that entire DataTable into a single list.

What is the VB syntax for this?
newbiewebSr. Software EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
What do you want the List to be? A List(Of String), where you combine the two columns to become one string or a List(Of SomeClass) where the class has two properties?
0
newbiewebSr. Software EngineerAuthor Commented:
I guess I want a dictionary:

Integer, String
0
Fernando SotoRetiredCommented:
Hi newbieweb;

Every key in a Dictionary(Of TKey, TValue) must be unique. Which will be the Key the column with the Integer or the column with the String data type?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

newbiewebSr. Software EngineerAuthor Commented:
key (integer) is unique
0
newbiewebSr. Software EngineerAuthor Commented:
Also, trying to find the Index by the Key, if you have a moment :)

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_28495175.html
0
Fernando SotoRetiredCommented:
Hi newbieweb;

I have made the following assumptions
Where dt is the instance name of the data table
      Column1 and Column2 are the names of the columns in the datatable

The below Linq to Object query will return a Dictionary(Of Integer, String)

Imports System.Linq

Dim results = _
    (From row In dt.AsEnumerable() _
     Select New With _
     { _
         .Col1 = row.Field(Of Integer)("Column1"), _
         .Col2 = row.Field(Of String)("Column2") _
     }).ToDictionary(Function(c1) c1.Col1, Function(c2) c2.Col2)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi newbiweb;

A Dictionary object does NOT maintain an index into its collection and therefor does not have an index. You may want to use a different collection if you need indexing.
0
newbiewebSr. Software EngineerAuthor Commented:
Thanks!
0
Fernando SotoRetiredCommented:
Not a problem newbieweb, glad I was able to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.