Link to home
Start Free TrialLog in
Avatar of dpdmembers
dpdmembersFlag for Barbados

asked on

In-Memory Query

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?
Avatar of Alan Warren
Alan Warren
Flag of Philippines image

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.

Alan
Avatar of dpdmembers

ASKER

In the Immediate window I get the following:

?finalQuery
In-Memory Query
    Results: Expanding will process the collection



When you expand the process you see all the records.
I have a attached a copy of what u see when u moused over the finalQuery
finalQuery.png
Avatar of 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.
Can u please send me a link with that example in VB preferably.  Thanks
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial