Late binding in query

671 views Asked by At

Yes I know what Option Strict does and I use it quite often. My issue is stemming from my Linq query and I can't seem to figure out how to get it from throwing up. I am wanting to get all DataRows in a given table where a row's id equals an id I give it. Also this query works just fine without Option Strict on a I get the rows I need, but I want to have it on.

The error: Option Strict on disallow's late binding

Here's what I have right now...

  Dim cRows() As DataRow = (From cRow In MasterDataSet.Tables(1).Rows Where cRow(childTableKey) = intParID).ToArray

The error is happening underneath: cRow(childTableKey)

I know what the error mean's as well, but can't seem to figure how to stop it from seeing it as an error. I have tried casting it and such already as well...

3

There are 3 answers

1
Greg On

Try using

In MasterDataSet.Tables(1) Where CType(cRow(childTableKey), Integer)

instead of

In MasterDataSet.Tables(1).Rows Where cRow(childTableKey)

The example below is working for me with Option Strict:

    Dim ds As New DataSet
    Dim dt As New DataTable
    ds.Tables.Add(dt)
    Dim dc As New DataColumn
    dc.DataType = GetType(System.Int32)
    dt.Columns.Add(dc)
    Dim dr As DataRow = dt.NewRow
    dr(0) = 10
    dt.Rows.Add(dr)

    Dim rows As DataRow() = (From r In ds.Tables(0) Where CType(r(0), Integer) = 10).ToArray
0
Maciej Los On

Try this:

Dim filteredTable1Data= MasterDataSet.Tables(1).AsEnumerable().
    Where(Function(r) r.Field(of Integer)("childTableKey")=intParID)

For further information, please see:
Queries in LINQ to DataSet
LINQ to DataSet Examples
Creating a DataTable From a Query (LINQ to DataSet)

0
ps2goat On

Without additional extensions, you need to copy the rows from the RowCollection to an Array. Then you can use LINQ.

Dim myRows(-1) as DataRow
MasterDataSet.Tables(1).Rows.CopyTo(myRows, 0)

 Dim cRows() As DataRow = (From cRow In myRows 
    Where CInt(cRow(childTableKey)) = intParID).ToArray()