简体   繁体   中英

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."

The problem is as follows I have a model with two value of type 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(). 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?' in the first place, or are they listed as strings? If the DataTable is treating those fields as strings, you will experience that error. The following code assumes a TestData DataSet with a Models DataRow, with two nullable string columns as YearBegin and 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. However, when I flip the types on the DataTable to nullable Int32, then the nearly identical code works properly:

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? type.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM