Reputation: 1300
I am getting a InvalidCast exception when I try to iterate through an IOrderedQueryable object as below.
appreciate any help:
IOrderedQueryable<Result> rs =
from res in db.Results
orderby res.Id
select res;
if (rs != null)
{
IEnumerator<Result> enumerator = rs.GetEnumerator();
try
{
while (enumerator.MoveNext())
{
Result r = enumerator.Current;
Console.WriteLine(r.BugId);
}
}
}
Run-time error:
System.InvalidCastException was unhandled
HResult=-2147467262
Message=Specified cast is not valid.
Source=System.Data
StackTrace:
at System.Data.SqlClient.SqlBuffer.get_Int32()
at System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
at Read_Result(ObjectMaterializer`1 )
at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
Upvotes: 0
Views: 559
Reputation: 2204
Seems that inside your Results
table you have column that is supposed to be an Int32
on DBContext
level but some of values that are inside it cannot be casted to Int32
. Make sure that your DBContext
matches table definition. Maybe you forgot to update it after some table schema changes...
Upvotes: 1
Reputation: 157048
The problem seems to be in the r.BugId
part.
The code of Result
seems to indicate it is an int
, but the returned value from the database isn't (maybe it is null
?). Hence, you receive this error message.
Check if BugId
is indeed an integer in the database, if not, and Result
is generated though code, try to regenerate.
Upvotes: 1