Page 1 of 1

Invalidcast exception on FirstOrDefault command

Posted: Thu 08 Nov 2012 13:18
by lgm42
I've an Invalid CastException when I run this code :

Code: Select all

helico = VE7SolSchemat.Physhels.Where(hel => hel.PhyshelId ==  physhelID).FirstOrDefault();
L'exception System.InvalidCastException s'est produite
HResult=-2147467262
Message=Le cast spécifié n'est pas valide.
Source=Devart.Data.Linq
StackTrace:
à Devart.Data.Linq.Engine.SingleKeyManager`2.a(Object[] A_0)
à Devart.Data.Linq.Engine.KeyManager`2.b(Object[] A_0)
à Devart.Data.Linq.Engine.ah.a(Expression A_0, Object& A_1)
à Devart.Data.Linq.Engine.ah.e(Expression A_0)
à Devart.Data.Linq.DataProvider.a(Expression A_0)
à Devart.Data.Linq.Engine.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
à System.Linq.Queryable.First[TSource](IQueryable`1 source)
à GSSDatabase.GSSRealDatabase.GetConsoCyclesHours(UInt32 physhelID) dans xx\GSSRealDatabaseTrends.cs:ligne 167
InnerException:

But When I change my code by :

Code: Select all

helico = VE7SolSchemat.Physhels.Where(hel => hel.PhyshelId == (int) physhelID).ToList()[0];
it works well.
So I don't understand why.

I use Devart for PostgreSQL and my devart.data.linq.dll file is under version 4.1.101.0

Re: Invalidcast exception on FirstOrDefault command

Posted: Thu 08 Nov 2012 13:20
by lgm42
If it can help you when I run

Code: Select all

helico = VE7SolSchemat.Physhels.Where(hel => hel.PhyshelId == physhelID).ToList().FirstOrDefault();
It's works well too.

Re: Invalidcast exception on FirstOrDefault command

Posted: Thu 08 Nov 2012 14:42
by MariiaI
There is a known issue with InvalidCastException. We are working on it now and will inform you when it is fixed.

Re: Invalidcast exception on FirstOrDefault command

Posted: Fri 09 Nov 2012 09:47
by lgm42
For the moment waht is the best way to prevent the error ?
Because making a ToList() is very poor in performance.

Re: Invalidcast exception on FirstOrDefault command

Posted: Mon 12 Nov 2012 10:23
by MariiaI
As a workaround you could try using a variable of the necessary type in the query. For example, if the 'PhyshelId' property is of type System.Int16 in your model, you should define the 'physhelID' variable as Int16, and so on:

Code: Select all

Int16 physhelID = 1;
helico = VE7SolSchemat.Physhels.Where(hel => hel.PhyshelId ==  physhelID).FirstOrDefault();
Please tell us if this helps.

Re: Invalidcast exception on FirstOrDefault command

Posted: Fri 30 Nov 2012 07:55
by MariiaI
The bug with the "Specified cast is not valid" exception when searching for an entity by its key is fixed. The fix is available in the new build of dotConnect for PostgreSQL 6.2.132.
It can be downloaded from http://www.devart.com/dotconnect/postgr ... nload.html (trial version) or from Registered Users' Area (for users with valid subscription only).
For more information, please refer to http://forums.devart.com/viewtopic.php?f=3&t=25385