Cast Exception in C# Where clause

133 Views Asked by At

It's been a while since I am trying to figure out what is causing a cast exception in this case. Following is my code.

var ratingTermDetails = dtRating.AsEnumerable()
    .Where(data => data.Field<int>("RATINGID") == ratingId)
    .FirstOrDefault();

dtRating is a datatable which gets populated from the database (oracle). The datatype of the column Ratingid is Integer in the database and the column is not nullable. The variable ratingId is an integer too.

2

There are 2 best solutions below

0
Ashish Samant On BEST ANSWER

As @MarcGravell said, the variable was getting converted into System.Decimal in C#. Upon changing to my code worked.

0
Aram Yako On

I don't know how much help this is because I've never seen similar Linq before. But in my understanding, you're saying that RATINGID is int in the DB and it's a primary key?

Then this should work.

var ratingTermDetails = dtRating
    .Where(data => data.Field.RATINGID == ratingId)
    .FirstOrDefault();