Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

LINQ with a Datatable

Posted on 2009-12-18
7
Medium Priority
?
428 Views
Last Modified: 2012-05-08
Experts,

How would I go about using LINQ to get results from an existing DataTable in VB.NET?
0
Comment
Question by:NigelRocks
  • 4
  • 3
7 Comments
 
LVL 10

Accepted Solution

by:
joriszwaenepoel earned 2000 total points
ID: 26081234
0
 
LVL 1

Author Comment

by:NigelRocks
ID: 26081343
I've attached their example.  I replaced the "from" line with my own:

From order In Me._dtGapDisplayInfo.AsEnumerable() , but I'm getting the error that System.Data.DataTable is not inumerable.  I've declared mine as:

Private _dtGapDisplayInfo As DataTable
 
Why would their datatable work and mine not work?

Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)

Dim orders As DataTable = ds.Tables("SalesOrderHeader")

Dim query = _
    From order In orders.AsEnumerable() _
    Where order.Field(Of Boolean)("OnlineOrderFlag") = True _
    Select New With { _
        .SalesOrderID = order.Field(Of Integer)("SalesOrderID"), _
        .OrderDate = order.Field(Of DateTime)("OrderDate"), _
        .SalesOrderNumber = order.Field(Of String)("SalesOrderNumber") _
     }

For Each onlineOrder In query
    Console.Write("Order ID: " & onlineOrder.SalesOrderID)
    Console.Write(" Order date: " & onlineOrder.OrderDate)
    Console.WriteLine(" Order number: " & onlineOrder.SalesOrderNumber)
Next
 

Open in new window

0
 
LVL 10

Assisted Solution

by:joriszwaenepoel
joriszwaenepoel earned 2000 total points
ID: 26081586
You probably need to import the DataTableExtensions and maybe also DataRowExtensions:

Imports System.Data.DataSetExtensions.DataTableExtensions
Imports System.Data.DataSetExtensions.DataRoxExtensions

 

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:NigelRocks
ID: 26096402
I've included the System.Data.DataSetExtensions .dll file in the references, but those namespaces won't resolve.  Do you know why that would be the case?
0
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 26096438
Adding the references in not enough.  You also need to use the Imports statements that I suggested.
0
 
LVL 1

Author Comment

by:NigelRocks
ID: 26096612
That's what I meant by "namespaces won't resolve".   I have used the import statements.
0
 
LVL 10

Assisted Solution

by:joriszwaenepoel
joriszwaenepoel earned 2000 total points
ID: 26096689
OK,

I just tried to create a simple demo.  It looks like you don't need the Imports, but you still need to reference the dll.  I can't test it right now to see if it actually works, but it seems to compile.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Screencast - Getting to Know the Pipeline
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

571 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