Reputation: 5
I have a project where I'm beginning to use LINQ for the first time and wanted to use the CopyToDataTable function. I know to use it I must add a reference to System.Data.DataSetExtentions, which I've done, here's the statements.
Imports System.Data
Imports System.Linq
Imports System.Data.SqlClient
Imports System.Data.DataSetExtensions
The last line gets error: "the namespace or type selected doesn't contain any public member or cannot be found".
Here's the linq code I'm using, which gets error "'CopyToDataTable' is not a member of 'System.Collections.Generic.IEnumerable(Of Object)'."
Dim qrySpec = _
From tblSpecs In dtAllResults.AsEnumerable()
Where tblSpecs.Field(Of String)("SPEC_DESC")
Order By tblSpecs.Item("SPEC_DESC")
Select tblSpecs.Item("SPEC_DESC") Distinct
Dim dt As DataTable = qrySpec.CopyToDataTable()
I'm currently running full .Net Framework 4.0 and have verified the reference has been added and it's version is 4.0.
Only thing I can find in my research is perhaps I need to revert down to .Net Framework 3.5, the file path is too long, or the SQL Server's .Net framework is pre-3.5. I really need to stay in 4.0 and I've tried copying the dll to a new/shorter path and referencing it there. Neither have worked. I am in the process of getting with the DBA's on the SQL Server info.
Any help would be most appreciated.
Upvotes: 0
Views: 806
Reputation: 9508
System.Data.DataSetExtensions
is not a namespace.
The CopyToDataTable
is indeed in System.Data.DataSetExtensions.dll
but the namespace is just System.Data
.
Edit:
Also, CopyToDataTable
is defined as this:
public static DataTable CopyToDataTable<T>(this IEnumerable<T> source) where T : DataRow;
Your IEnumerable from your query must be of type DataRow
. So, you would either need to somehow return your results as DataRow from the query.
The current issue is that you are not selecting your row, you are using the Item
method to select a set of Objects.
EDIT2:
If all you are after is a DataTable with distinct SPEC_DESC
in it, then perhaps this would do:
DataTable table2 = new DataTable();
table2.Columns.Add("SPEC_DESC", typeof(string));
foreach (var desc in (from row in table.AsEnumerable()
select (string) row["SPEC_DESC"]).Distinct())
{
table2.Rows.Add(desc);
}
Upvotes: 1