LINQ with a Datatable

Posted on 2009-12-18
Last Modified: 2012-05-08

How would I go about using LINQ to get results from an existing DataTable in VB.NET?
Question by:NigelRocks
    LVL 10

    Accepted Solution

    LVL 1

    Author Comment

    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.
    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)

    Open in new window

    LVL 10

    Assisted Solution

    You probably need to import the DataTableExtensions and maybe also DataRowExtensions:

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


    LVL 1

    Author Comment

    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?
    LVL 10

    Expert Comment

    Adding the references in not enough.  You also need to use the Imports statements that I suggested.
    LVL 1

    Author Comment

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

    Assisted Solution


    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.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now