LINQ with a Datatable

Experts,

How would I go about using LINQ to get results from an existing DataTable in VB.NET?
LVL 1
NigelRocksAsked:
Who is Participating?
 
joriszwaenepoelCommented:
0
 
NigelRocksAuthor Commented:
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
 
joriszwaenepoelCommented:
You probably need to import the DataTableExtensions and maybe also DataRowExtensions:

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

 

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
NigelRocksAuthor Commented:
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
 
joriszwaenepoelCommented:
Adding the references in not enough.  You also need to use the Imports statements that I suggested.
0
 
NigelRocksAuthor Commented:
That's what I meant by "namespaces won't resolve".   I have used the import statements.
0
 
joriszwaenepoelCommented:
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
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.

All Courses

From novice to tech pro — start learning today.