-1

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

Trevor
  • 7,777
  • 6
  • 31
  • 50
  • You need to cast the row's value to an integer. Does this work for you? `Where Cint(cRow(childTableKey)) = intParID` – ps2goat Mar 20 '15 at 20:26
  • @ps2goat no it doesn't I have tried that, thanks though. – Trevor Mar 20 '15 at 20:30
  • It's great that you're familiar with Option Strict...but what error are you receiving? – Greg Mar 20 '15 at 20:40
  • @Greg sorry about that I removed it from my title and was going to put that in my post and forgot... – Trevor Mar 20 '15 at 20:45
  • Try doing a `ToList` -- I think I had issues with a RowCollection and LINQ before. Converting it to a list gets rid of that issue. `From cRow In MasterDataSet.Tables(1).Rows.ToList()` – ps2goat Mar 20 '15 at 20:57
  • @ps2goat that doesn't work as "ToList()" isn't a member of rows... – Trevor Mar 20 '15 at 21:01
  • Try `From cRow In MasterDataSet.Tables(1).AsEnumerable()...`. – Mark Mar 20 '15 at 21:02
  • @Mark "AsEnumerable" isn't a member of DataTable... – Trevor Mar 20 '15 at 21:03
  • 1
    Sorry, should have linked to [the docs](https://msdn.microsoft.com/en-us/library/system.data.datatableextensions.asenumerable%28v=vs.110%29.aspx). You need to reference `System.Data.DataSetExtensions.dll`. – Mark Mar 20 '15 at 21:04
  • Also, you usually need `Option Infer On` for LINQ, but maybe you already have that, can't tell. – Mark Mar 20 '15 at 21:07
  • This seems to be unrealated to `Option Stict` instruction... – Maciej Los Mar 20 '15 at 21:21
  • @Mark, you don't need `Option Infer On` for LINQ. – ps2goat Mar 20 '15 at 21:26
  • @Mark well I'll be darn! It was because the "System.Data.DataSetExtensions.dll" wasn't in the references. We updated a project to the 4.0 framework recently from 2.0 I guess it didn't carry these over, but that was it! Thanks! – Trevor Mar 20 '15 at 21:34
  • @Mark also the reason it was working in my test solution was because it was a new project which included these references... – Trevor Mar 20 '15 at 21:37
  • @ps2goat Thanks! I would have sworn I've run into issues with that, but maybe it was related to anonymous types? – Mark Mar 20 '15 at 21:48
  • @Mark, yes that would be it. We tend to steer away from anonymous types because of the confusion they can cause. You can always use a `Tuple`, but it's just as easy to create your own object and retain clarity. – ps2goat Mar 20 '15 at 21:54

3 Answers3

0

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
Greg
  • 414
  • 4
  • 12
  • Have you tried both not using .Rows and casting cRow(childTableKey) as integer? Do you have the System.Data.DataSetExtensions reference? – Greg Mar 20 '15 at 21:16
0

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)

ps2goat
  • 8,067
  • 1
  • 35
  • 68
Maciej Los
  • 8,468
  • 1
  • 20
  • 35
0

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()
ps2goat
  • 8,067
  • 1
  • 35
  • 68