简体   繁体   中英

I need help trying to work with a row accessed from a datatable using LINQ

I am trying to get a single row from a data table using LINQ. It seems to getting the record but I am not able to work with it. Not sure what I am doing wrong. My LINQ query is below:

IEnumerable<DataRow> query = 
      from myRow in dataTable.AsEnumerable()
      where myRow.Field<int>("AM2MIN") <= Convert.ToInt32(minimumValue) &&
            myRow.Field<int>("AM2MAX") >= Convert.ToInt32(minimumValue)
      select myRow;

When I do the following to see if any records exist, it errors out.

if (query.Any())
{

}

InvalidCastException seems to be the consistent error.

Any help is much appreciated!

Looks like one of your fields does not contain integer value.

Verify Data Type of those columns. Also verify if Nulls are allowed for them. If data type is int , but nulls are allowed, then cast to nullable int: myRow.Field<int?>("AM2MIN") and verify if it has value.

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