Asp Core 1.1, DataTable存在于两者中
Asp Core 1.1, DataTable exists in both
我正在开发一个 dot net core 1.1 应用程序,我试图在其中使用 Accord.Net. According to examples in this page (Naive Bayes) 我需要将从 DB 检索到的数据转换为 DataTable。
问题是,在使用 DataTable 时出现此错误:
The type 'DataTable' exists in both 'Shim, ...' and
'System.Data.Common, ...'
即使我用这个:
DataTable learningDataNotCodifiedAsDataTable = new DataTable();
或者这个:
System.Data.DataTable learningDataNotCodifiedAsDataTable = new System.Data.DataTable();
TG.
如果你在Assemblies中有System.Data
个程序集并且不想或不能删除它,那么你可以使用extern alias, but when I bypassed this error using it I got 'DataTable' does not contain a constructor that takes 0/1 arguments
error, and if believe this discussion绕过它,原因是:
System.Data.DataTable is present in .Net core(1.0,1.1) as an empty class to
complete the interfaces implementation. This issue is to track the
work needed to bring in an API to provide DataTable like API in .Net
Core.
并且它仅在 .NET Core 2.0 中发生了变化,请参阅 this SO post。我试过你在 .NET Core 2.0 项目(在 VS 2017 15.3 中)中编写代码,然后才工作正常。
更新:
我的意思是这个程序集。
但是正如你所说你只有 NUGET 包,那么你也可以在你的 csproj
文件中为 Nuget 包使用别名,如下所示(我使用 System.Data.Common
你可以用你的 Shim 包替换它如果需要):
<Target Name="DataAlias" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
<ItemGroup>
<ReferencePath Condition="'%(FileName)' == 'System.Data.Common'">
<Aliases>MyData</Aliases>
</ReferencePath>
</ItemGroup>
</Target>
然后像这样在 C# 中引用它:
extern alias MyData; //1st line in .cs file
...
using MyData::System.Data;
...
DataTable datatable = new DataTable();
但是你仍然无法使用,因为你会得到我上面写的关于构造函数的错误。在这里你有 2 个选项来解决这个问题:
- 切换到 .NET Core 2.0
- 如果适合您,请尝试使用
DbDataReader
的变通解决方案 from this post
虽然 DataTable 在 .NET Core 1.1 中不可用,但现在在 .NET Core 2.0 中可用。如果您可以将您的项目升级到 .NET Core 2.0,那么您将能够在您的代码中使用它。
但是,如果您现在不能切换到 .NET Core 2.0,请注意您不需要将 DataTables 与 Accord.NET 框架中的任何方法一起使用。给出或显示它们只是因为它们可以提供一些额外的便利,但并不是真正需要它们,如下例所示:
string[] columnNames = { "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis" };
string[][] data =
{
new string[] { "Sunny", "Hot", "High", "Weak", "No" },
new string[] { "Sunny", "Hot", "High", "Strong", "No" },
new string[] { "Overcast", "Hot", "High", "Weak", "Yes" },
new string[] { "Rain", "Mild", "High", "Weak", "Yes" },
new string[] { "Rain", "Cool", "Normal", "Weak", "Yes" },
new string[] { "Rain", "Cool", "Normal", "Strong", "No" },
new string[] { "Overcast", "Cool", "Normal", "Strong", "Yes" },
new string[] { "Sunny", "Mild", "High", "Weak", "No" },
new string[] { "Sunny", "Cool", "Normal", "Weak", "Yes" },
new string[] { "Rain", "Mild", "Normal", "Weak", "Yes" },
new string[] { "Sunny", "Mild", "Normal", "Strong", "Yes" },
new string[] { "Overcast", "Mild", "High", "Strong", "Yes" },
new string[] { "Overcast", "Hot", "Normal", "Weak", "Yes" },
new string[] { "Rain", "Mild", "High", "Strong", "No" },
};
// Create a new codification codebook to
// convert strings into discrete symbols
Codification codebook = new Codification(columnNames, data);
// Extract input and output pairs to train
int[][] symbols = codebook.Transform(data);
int[][] inputs = symbols.Get(null, 0, -1); // Gets all rows, from 0 to the last (but not the last)
int[] outputs = symbols.GetColumn(-1); // Gets only the last column
// Create a new Naive Bayes learning
var learner = new NaiveBayesLearning();
NaiveBayes nb = learner.Learn(inputs, outputs);
// Consider we would like to know whether one should play tennis at a
// sunny, cool, humid and windy day. Let us first encode this instance
int[] instance = codebook.Translate("Sunny", "Cool", "High", "Strong");
// Let us obtain the numeric output that represents the answer
int c = nb.Decide(instance); // answer will be 0
// Now let us convert the numeric output to an actual "Yes" or "No" answer
string result = codebook.Translate("PlayTennis", c); // answer will be "No"
// We can also extract the probabilities for each possible answer
double[] probs = nb.Probabilities(instance); // { 0.795, 0.205 }
我正在开发一个 dot net core 1.1 应用程序,我试图在其中使用 Accord.Net. According to examples in this page (Naive Bayes) 我需要将从 DB 检索到的数据转换为 DataTable。
问题是,在使用 DataTable 时出现此错误:
The type 'DataTable' exists in both 'Shim, ...' and 'System.Data.Common, ...'
即使我用这个:
DataTable learningDataNotCodifiedAsDataTable = new DataTable();
或者这个:
System.Data.DataTable learningDataNotCodifiedAsDataTable = new System.Data.DataTable();
TG.
如果你在Assemblies中有System.Data
个程序集并且不想或不能删除它,那么你可以使用extern alias, but when I bypassed this error using it I got 'DataTable' does not contain a constructor that takes 0/1 arguments
error, and if believe this discussion绕过它,原因是:
System.Data.DataTable is present in .Net core(1.0,1.1) as an empty class to complete the interfaces implementation. This issue is to track the work needed to bring in an API to provide DataTable like API in .Net Core.
并且它仅在 .NET Core 2.0 中发生了变化,请参阅 this SO post。我试过你在 .NET Core 2.0 项目(在 VS 2017 15.3 中)中编写代码,然后才工作正常。
更新: 我的意思是这个程序集。
但是正如你所说你只有 NUGET 包,那么你也可以在你的 csproj
文件中为 Nuget 包使用别名,如下所示(我使用 System.Data.Common
你可以用你的 Shim 包替换它如果需要):
<Target Name="DataAlias" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
<ItemGroup>
<ReferencePath Condition="'%(FileName)' == 'System.Data.Common'">
<Aliases>MyData</Aliases>
</ReferencePath>
</ItemGroup>
</Target>
然后像这样在 C# 中引用它:
extern alias MyData; //1st line in .cs file
...
using MyData::System.Data;
...
DataTable datatable = new DataTable();
但是你仍然无法使用,因为你会得到我上面写的关于构造函数的错误。在这里你有 2 个选项来解决这个问题:
- 切换到 .NET Core 2.0
- 如果适合您,请尝试使用
DbDataReader
的变通解决方案 from this post
虽然 DataTable 在 .NET Core 1.1 中不可用,但现在在 .NET Core 2.0 中可用。如果您可以将您的项目升级到 .NET Core 2.0,那么您将能够在您的代码中使用它。
但是,如果您现在不能切换到 .NET Core 2.0,请注意您不需要将 DataTables 与 Accord.NET 框架中的任何方法一起使用。给出或显示它们只是因为它们可以提供一些额外的便利,但并不是真正需要它们,如下例所示:
string[] columnNames = { "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis" };
string[][] data =
{
new string[] { "Sunny", "Hot", "High", "Weak", "No" },
new string[] { "Sunny", "Hot", "High", "Strong", "No" },
new string[] { "Overcast", "Hot", "High", "Weak", "Yes" },
new string[] { "Rain", "Mild", "High", "Weak", "Yes" },
new string[] { "Rain", "Cool", "Normal", "Weak", "Yes" },
new string[] { "Rain", "Cool", "Normal", "Strong", "No" },
new string[] { "Overcast", "Cool", "Normal", "Strong", "Yes" },
new string[] { "Sunny", "Mild", "High", "Weak", "No" },
new string[] { "Sunny", "Cool", "Normal", "Weak", "Yes" },
new string[] { "Rain", "Mild", "Normal", "Weak", "Yes" },
new string[] { "Sunny", "Mild", "Normal", "Strong", "Yes" },
new string[] { "Overcast", "Mild", "High", "Strong", "Yes" },
new string[] { "Overcast", "Hot", "Normal", "Weak", "Yes" },
new string[] { "Rain", "Mild", "High", "Strong", "No" },
};
// Create a new codification codebook to
// convert strings into discrete symbols
Codification codebook = new Codification(columnNames, data);
// Extract input and output pairs to train
int[][] symbols = codebook.Transform(data);
int[][] inputs = symbols.Get(null, 0, -1); // Gets all rows, from 0 to the last (but not the last)
int[] outputs = symbols.GetColumn(-1); // Gets only the last column
// Create a new Naive Bayes learning
var learner = new NaiveBayesLearning();
NaiveBayes nb = learner.Learn(inputs, outputs);
// Consider we would like to know whether one should play tennis at a
// sunny, cool, humid and windy day. Let us first encode this instance
int[] instance = codebook.Translate("Sunny", "Cool", "High", "Strong");
// Let us obtain the numeric output that represents the answer
int c = nb.Decide(instance); // answer will be 0
// Now let us convert the numeric output to an actual "Yes" or "No" answer
string result = codebook.Translate("PlayTennis", c); // answer will be "No"
// We can also extract the probabilities for each possible answer
double[] probs = nb.Probabilities(instance); // { 0.795, 0.205 }