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

时间:2016-07-14 10:39:55

标签: c# wpf linq linq-to-dataset

在通过过滤将Datatable转换为Dataview时,查询我的SQL数据库以获取数据集中的详细信息,因为作为Linq的新手,我收到了基本的转换错误。

代码:

/usr/local/bin/gcc-5

例外是

DataView cktDv = (from clounceform in dsclounceForms.Tables["Table2"].AsEnumerable()
                                         where clounceform.Field<string>("Form_FileType_ID").Equals("5")
                                         select clounceform).CopyToDataTable().AsDataView();

我无法继续进行,实际上将字符串的转换更改为int并给出了不同的错误,这更加误导了我 当试图转换为int

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

错误:

DataView cktDataView = (from clounceform in dsclounceForms.Tables["Table2"].AsEnumerable()
                                         where clounceform.Field<int>("Form_FileType_ID") == 5
                                         select clounceform).CopyToDataTable().AsDataView();

1 个答案:

答案 0 :(得分:5)

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

我怀疑这会奏效:

where clounceform.Field<byte>("Form_FileType_ID") == (byte)5