无法将类型 'System.Byte' 的对象转换为类型 'System.String'
Unable to cast object of type 'System.Byte' to type 'System.String'
在通过过滤将 Datatable 转换为 Dataview 时查询我的 SQL 数据库以将详细信息提取到 Dataset 中,因为是 Linq 的新手,我收到基本转换错误。
代码:
DataView cktDv = (from clounceform in dsclounceForms.Tables["Table2"].AsEnumerable()
where clounceform.Field<string>("Form_FileType_ID").Equals("5")
select clounceform).CopyToDataTable().AsDataView();
例外是
Unable to cast object of type 'System.Byte' to type 'System.String'.
System.Data.DataSetExtensions
at System.Data.DataRowExtensions.UnboxT`1.ReferenceField(Object value)
at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
at ClounceFormsSuite.Design.CreateFormDesign.<>c.<CreateForm>b__12_0(DataRow bounceform) in myprojectfolderpath\CreateFormDesign.xaml.cs:line 115
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at System.Data.DataTableExtensions.LoadTableFromEnumerable[T](IEnumerable`1 source, DataTable table, Nullable`1 options, FillErrorEventHandler errorHandler)
at System.Data.DataTableExtensions.CopyToDataTable[T](IEnumerable`1 source)
at ClounceFormsSuite.Design.CreateFormDesign.CreateForm(String[] cktFileContent, String filePath, String formID) in myprojectfolderpath\CreateFormDesign.xaml.cs:line 114
at ClounceFormsSuite.Design.CreateFormDesign.UserControl_Loaded(Object sender, RoutedEventArgs e) in myprojectfolderpath\CreateFormDesign.xaml.cs:line 70
我无法继续进行,我实际上将字符串转换为 int 并给出了不同的错误,这更误导了我
当尝试转换为 int
DataView cktDataView = (from clounceform in dsclounceForms.Tables["Table2"].AsEnumerable()
where clounceform.Field<int>("Form_FileType_ID") == 5
select clounceform).CopyToDataTable().AsDataView();
错误:
Specified cast is not valid.
数据是 byte
,不是 string
,也不是 int
。因此,您需要使用 Field<byte>(...)
:
我怀疑这会起作用:
where clounceform.Field<byte>("Form_FileType_ID") == (byte)5
在通过过滤将 Datatable 转换为 Dataview 时查询我的 SQL 数据库以将详细信息提取到 Dataset 中,因为是 Linq 的新手,我收到基本转换错误。
代码:
DataView cktDv = (from clounceform in dsclounceForms.Tables["Table2"].AsEnumerable()
where clounceform.Field<string>("Form_FileType_ID").Equals("5")
select clounceform).CopyToDataTable().AsDataView();
例外是
Unable to cast object of type 'System.Byte' to type 'System.String'.
System.Data.DataSetExtensions
at System.Data.DataRowExtensions.UnboxT`1.ReferenceField(Object value)
at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
at ClounceFormsSuite.Design.CreateFormDesign.<>c.<CreateForm>b__12_0(DataRow bounceform) in myprojectfolderpath\CreateFormDesign.xaml.cs:line 115
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at System.Data.DataTableExtensions.LoadTableFromEnumerable[T](IEnumerable`1 source, DataTable table, Nullable`1 options, FillErrorEventHandler errorHandler)
at System.Data.DataTableExtensions.CopyToDataTable[T](IEnumerable`1 source)
at ClounceFormsSuite.Design.CreateFormDesign.CreateForm(String[] cktFileContent, String filePath, String formID) in myprojectfolderpath\CreateFormDesign.xaml.cs:line 114
at ClounceFormsSuite.Design.CreateFormDesign.UserControl_Loaded(Object sender, RoutedEventArgs e) in myprojectfolderpath\CreateFormDesign.xaml.cs:line 70
我无法继续进行,我实际上将字符串转换为 int 并给出了不同的错误,这更误导了我 当尝试转换为 int
DataView cktDataView = (from clounceform in dsclounceForms.Tables["Table2"].AsEnumerable()
where clounceform.Field<int>("Form_FileType_ID") == 5
select clounceform).CopyToDataTable().AsDataView();
错误:
Specified cast is not valid.
数据是 byte
,不是 string
,也不是 int
。因此,您需要使用 Field<byte>(...)
:
我怀疑这会起作用:
where clounceform.Field<byte>("Form_FileType_ID") == (byte)5