无法将类型为“System.Byte”的对象强制转换为类型“System.String”

本文关键字:System 类型 String 转换 对象 Byte | 更新日期: 2023-09-27 17:57:00

通过过滤将数据表转换为数据视图时,查询了我的SQL数据库以将详细信息提取到数据集中,因为是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.

无法将类型为“System.Byte”的对象强制转换为类型“System.String”

数据是byte,不是string,也不是int。因此,您需要使用Field<byte>(...)

我怀疑这会起作用:

where clounceform.Field<byte>("Form_FileType_ID") == (byte)5
相关文章: