I’m using a LINQ query on a DataTable where a data field may be a bool (true/false) or an int (1/0). The LINQ query asks for a type for the Field method and if the data field differs, an InvalidCastException is thrown (which cannot be caught).
var query =
from tbl12 in t12.AsEnumerable()
where tbl12.Field<int>("theField") == 0
select new { T12 = tbl12 };
or
var query =
from tbl12 in t12.AsEnumerable()
where tbl12.Field<bool>("theField") == false
select new { T12 = tbl12 };
How can I get around this? I want to filter results where the field = false or 0.
I have found the answer: set the type as
objectand use the ToString() method on the field.ie: