Hi All,
I'm having an issue getting linq to work in a script task. I'm using sql server 2012 data tools (vs2010). I'm able to add the system.linq to the imports but when I attempt to query a data table I get the following error:
Error1
Expression of type 'System.Data.DataTable' is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the LINQ provider.'
I have tried the following without success
1) Adding system.linq to my imports in the task
2) adding system.data.linq to my references
3) I have confirmed that I'm running against the 4.0 framework
4) I have tried to add system.core but I get the following prompt
"A reference to 'System.Core' could not be added. This component is already automatically referenced by the build system'
This is what I'm attempting to do in my package
1) Use a dataflow task that consists of
1a) a ole db source that selects records from a table
1b) a recordset destination that reads the results into a user variable
2) The dataflow task is attached to a script task that accepts the user variable
The script then converts the recordset into a datatable and iterates through it. Here is the code it works except for the linq references
The issue is that
#Region "Imports"
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Linq
#End Region
Public Sub Main()
Dim MyOleAdapter As New OleDb.OleDbDataAdapter
Dim MyDataTable As New DataTable
MyOleAdapter.Fill(MyDataTable, Dts.Variables("records").Value)
'This is here just to confirm that the data is present in the data table which I do
MessageBox.Show(MyDataTable.Rows.Count.ToString)
For Each myrow As DataRow In MyDataTable.Rows
MessageBox.Show(myrow.Item("FirstName").ToString)
Next
'This is where I run into trouble intellisense does not give me the AsEnumerable in the following attempt to use linq
Dim Test = From r In MyDataTable
Where r.item("FirstName") = "Chad"
Select r
This error is returned: Error1Expression of type 'System.Data.DataTable' is not queryable
If I add the .AsEnumerable Iget the following error:
Error2
'asEnumerable' is not a member of 'System.Data.DataTable'
Dts.TaskResult = ScriptResults.Success
End Sub
I created the same code block in a windows form and it runs without issue meaning using the same imports and the same code. Here is the code that runs from the windows form application:
Imports System.Data
Imports System.Linq
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim MyAdapter As New OleDb.OleDbDataAdapter("Select top 10 * from table", "Provider=SQLOLEDB;Data Source=;Persist Security Info=True;Password=;User ID=L;Initial Catalog=)
Dim MyDateSet As New DataSet
MyAdapter.Fill(MyDateSet, "records")
MessageBox.Show(MyDateSet.Tables("records").Rows.Count.ToString)
For Each myrow As DataRow In MyDateSet.Tables("records").Rows
MessageBox.Show(myrow.Item("FirstName").ToString)
Next
Dim Test = From r In MyDateSet.Tables("records").AsEnumerable
Where r.Item("FirstName") = "Chad"
Select r
For Each myrow As DataRow In Test
MessageBox.Show(myrow.Item("LastName").ToString)
Next
I'm sure this has something to do with references but I can't seem to find the issue or track it down online. Any help would be greatly appreciated.
Dirk