[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
SolvedPrivate

Visual Basic: Converting from DataTable to List

Posted on 2014-08-11
9
Medium Priority
?
34 Views
Last Modified: 2016-02-18
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?
0
Comment
Question by:newbieweb
  • 5
  • 4
9 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40253830
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
 

Author Comment

by:newbieweb
ID: 40253863
I guess I want a dictionary:

Integer, String
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40253879
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
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 

Author Comment

by:newbieweb
ID: 40253907
key (integer) is unique
0
 

Author Comment

by:newbieweb
ID: 40253998
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
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 40254013
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40254061
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
 

Author Closing Comment

by:newbieweb
ID: 40254131
Thanks!
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40254133
Not a problem newbieweb, glad I was able to help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month18 days, 1 hour left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question