简体   繁体   English

Linq To数据集错误System.InvalidCastException:指定的强制转换无效

[英]Linq To Data set error System.InvalidCastException: Specified cast is not valid

I get the following exception using Linq to data set "System.InvalidCastException: Specified cast is not valid." 我使用Linq到数据集“ System.InvalidCastException:指定的转换无效”时出现以下异常。

The problem is as follows I have a model with two value of type int?. 问题如下,我有一个类型为int?的两个值的模型。 The values in the database are not required so some fields are blank. 数据库中的值不是必需的,因此某些字段为空白。 I have read the table into a data set and now I need to query the data set using the following code. 我已将表读入数据集,现在需要使用以下代码查询数据集。

//model
public class Model
{
    // Public Properties
    ...
    ...
    ...
    public int? YearBegin { get; set; }
    public int? YearEnd { get; set; }
}

//query
var list = from m in data.Tables["Models"].AsEnumerable()
select new Model
{
   // rest of members omitted to simplify
   YearBegin = m.Field<int>("YearBegin"), 
   YearEnd =   m.Field<int>("YearEnd") 
};

I have tried the following none have worked: 我尝试了以下方法,但均未起作用:

m.Field<int?>("YearBegin")
YearEnd = m.IsNull("YearEnd") ? null, m.Field<int>("YearEnd")

Is there another way to check if the field has a value similar to String.IsNullOrEmpty(). 还有另一种方法来检查该字段是否具有类似于String.IsNullOrEmpty()的值。 Using string as the type is not a possibility... 使用字符串作为类型是不可能的...

Thanks 谢谢

You aren't using a typed DataSet, so my first question would be is the does the DataTable know that those fields are supposed to be 'int?' 您没有使用类型化的DataSet,所以我的第一个问题是DataTable是否知道这些字段应该为“ int”? in the first place, or are they listed as strings? 首先还是它们被列为字符串? If the DataTable is treating those fields as strings, you will experience that error. 如果DataTable将这些字段视为字符串,则会遇到该错误。 The following code assumes a TestData DataSet with a Models DataRow, with two nullable string columns as YearBegin and YearEnd: 以下代码假定具有Models DataRow的TestData DataSet,其中两个可空字符串列为YearBegin和YearEnd:

using (TestData ds = new TestData())
{
      // Typed Rows
      ds.Models.AddModelsRow("1", "2");
      ds.Models.AddModelsRow(ds.Models.NewModelsRow()); // NULL INFO TEST
      // Untyped rows
      DataRow r = ds.Models.NewRow();
      r[0] = "4";
      r[1] = "5";
      ds.Models.Rows.Add(r);


      //query
      var list = from m in ds.Tables["Models"].AsEnumerable()
                 select new Model
                 {
                     // rest of members omitted to simplify
                     YearBegin = m.Field<int?>("YearBegin"),
                     YearEnd = m.Field<int?>("YearEnd"),
                 };
}

That code will encounter the InvalidCastException. 该代码将遇到InvalidCastException。 However, when I flip the types on the DataTable to nullable Int32, then the nearly identical code works properly: 但是,当我将DataTable上的类型翻转为可为null的Int32时,几乎相同的代码可以正常工作:

using (TestData ds = new TestData())
{
      // Typed Rows
      ds.Models.AddModelsRow(1, 2);
      ds.Models.AddModelsRow(ds.Models.NewModelsRow()); // NULL INFO TEST
      // Untyped rows
      DataRow r = ds.Models.NewRow();
      r[0] = 4;
      r[1] = 5;
      ds.Models.Rows.Add(r);


      //query
      var list = from m in ds.Tables["Models"].AsEnumerable()
                 select new Model
                 {
                     // rest of members omitted to simplify
                     YearBegin = m.Field<int?>("YearBegin"),
                     YearEnd = m.Field<int?>("YearEnd"),
                 };
}

Take a look at your DataTable. 看一下您的数据表。 You can correct your issue there. 您可以在那里改正问题。 The Field cast to int? 字段转换为整数? will not work unless your DataTable field matches the int? 除非您的DataTable字段与int匹配,否则将无法正常工作? type. 类型。

问题已解决,我正在使用旧式访问数据库,并且数据类型存储为Integer而不是存储在Long Integer上,这意味着它在数据集中表示为Int16,因此无效的强制转换异常...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.InvalidCastException:指定的强制转换无效(linq查询) - System.InvalidCastException: Specified cast is not valid (linq query) XML文件错误错误:System.InvalidCastException:指定的强制转换无效 - XML File Error Error : System.InvalidCastException: Specified cast is not valid 使用LINQ to SQL和sqlmetal的C#中的错误System.InvalidCastException:指定的强制转换无效 - Error in C# using LINQ to SQL and sqlmetal System.InvalidCastException: Specified cast is not valid 错误:GetCharacters System.InvalidCastException:指定的强制类型转换无效的服务器 - Error: GetCharacters System.InvalidCastException: Specified cast is not valid server System.InvalidCastException:指定的强制转换无效。错误 - System.InvalidCastException: Specified cast is not valid. Error 消息错误:System.InvalidCastException:指定的强制转换无效10 - Message error : System.InvalidCastException: Specified cast is not valid 10 指定的转换无效-System.InvalidCastException - Specified cast is not valid - System.InvalidCastException System.InvalidCastException:指定的强制转换无效 - System.InvalidCastException: Specified cast is not valid “ System.InvalidCastException:指定的转换无效”-DateTime - “System.InvalidCastException: Specified cast is not valid” - DateTime System.InvalidCastException:指定的强制转换在.ExecuteScalar上无效 - System.InvalidCastException: Specified cast is not valid at .ExecuteScalar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM