Basically I have a list:
List<Decimal> SortOrders = new List<Decimal>();
And a DataRow.
I wan to compare whether the list contains an item from a control.
But I got an exception,
cannot convert from “Object ” to “decimal
DataRow dr = dataSetSomething.Tables[0].NewRow();
...
dr["SortOrder"] = Convert.ToDecimal(numericOrder.Value);// works fine here
if (SortOrders.Contains(dr["SortOrder"]))// exception here, Why?
{
}
Thanks for help.
The type of
dr["SortOrder"]isObject.Any type in .NET is an object (as all types inherit from
Object, directly or through the inheritance chain), so assigning adecimalto anobjectis fine.The opposite is not true – not all
objects aredecimal.You need to cast in order to get the expected behaviour:
(which will throw an exception if the underlying type is not a
decimal).