In-Memory Query

Posted on 2012-09-05
Last Modified: 2012-09-05
I have the following code:

            Dim xAll = (From xx In _Context.Vessel_Owners
                        Select xx)

            Dim xSearch1 = (From xx In _Context.Vessel_Owners
                           Where xx.Natregno = txtNatregnoSearch.Text.ToString()
                            Select xx)

            Dim xSearch2 = (From xx In _Context.Vessel_Owners
                          Where xx.Passport_No = txtPassportNoSearch.Text.ToString()
                           Select xx)

            Dim xSearch3 = (From xx In _Context.Vessel_Owners
                           Where xx.Owner_Surname = txtOwnerSurnameSearch.Text.ToString()
                            Select xx)

            Dim xSearch4 = (From xx In _Context.Vessel_Owners
                           Where xx.Owner_Firstname = txtOwnerFirstnameSearch.Text.ToString()
                            Select xx)

            Dim finalQuery = CreateEmptyEnumerable(xAll)

            If Not String.IsNullOrEmpty(txtNatregnoSearch.Text.ToString()) Then

                finalQuery = xSearch1

            End If
            If Not String.IsNullOrEmpty(txtPassportNoSearch.Text.ToString()) Then

                finalQuery = finalQuery.Union(xSearch2)
            End If
            If Not String.IsNullOrEmpty(txtOwnerSurnameSearch.Text.ToString()) Then

                finalQuery = finalQuery.Union(xSearch3)
            End If
            If Not String.IsNullOrEmpty(txtOwnerFirstnameSearch.Text.ToString()) Then

                finalQuery = finalQuery.Union(xSearch4)
            End If

The finalQuery is producing an In-Memory Query which I am trying to transform into a Datatable.  

When I do the following:    Dim dr = TryCast(finalQuery , DataTable)
dr is returning Nothing

How can i get the  finalQuery into a Datatable?
Question by:dpdmembers
    LVL 26

    Expert Comment

    by:Alan Warren
    can you put a break on the line Dim dr = TryCast(finalQuery , DataTable)  and then in the Immediate window: type, ?finalQuery and hit enter.

    Would be nice to see if that finalQuery is syntactically correct.


    Author Comment

    In the Immediate window I get the following:

    In-Memory Query
        Results: Expanding will process the collection

    When you expand the process you see all the records.

    Author Comment

    I have a attached a copy of what u see when u moused over the finalQuery
    LVL 62

    Expert Comment

    by:Fernando Soto
    Hi dpdmembers

    The problem you are having is that the type Vessel_Owners can not be converted / cast to a datatable. The reason it is not throwing an error is that the TryCast does not throw an exception if an error happens it will return Nothing when an exception has happened.

    In order to convert it to a DataTable you will need to iterate over the result set and get a new row from the DataTable by using DataTableInstance.NewRow() and then filling in the column data. But before starting you will need to create a DataTable with all of its columns defined.

    Author Comment

    Can u please send me a link with that example in VB preferably.  Thanks
    LVL 62

    Accepted Solution

    Hi dpdmembers;

    Here is some sample code.

    ' Create the DataTable object
    Dim dt = New DataTable()
    ' Create one column for each column in source object and
    ' giving it the correct data type
    Dim column = New DataColumn("CompanyName", GetType(System.String))
    ' Add the column to the DataTable
    column = New DataColumn("ContactName", GetType(System.String))
    column = New DataColumn("Address", GetType(System.String))
    column = New DataColumn("City", GetType(System.String))
    column = New DataColumn("Country", GetType(System.String))
    column = New DataColumn("Phone", GetType(System.String))
    column = New DataColumn("Fax", GetType(System.String))
    ' Linq to SQL data context
    Dim ctx = New DataClasses1DataContext()
    ' Get data
    Dim allCustomers = From c In ctx.Customers
                       Select c
    ' Iterate through the result set and assign to a row and add to DataTable
    For Each c As Customer In allCustomers
        Dim row = dt.NewRow()
        row("CompanyName") = c.CompanyName
        row("ContactName") = c.ContactName
        row("Address") = c.Address
        row("City") = c.City
        row("Country") = c.Country
        row("Phone") = c.Phone
        row("Fax") = c.Fax
    ' Now you can display in Gridview
    DataGridView1.DataSource = dt

    Open in new window


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    734 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

    26 Experts available now in Live!

    Get 1:1 Help Now