Invalidcast exception on FirstOrDefault command

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
lgm42
Posts: 4
Joined: Thu 10 Mar 2011 08:28

Invalidcast exception on FirstOrDefault command

Post by lgm42 » Thu 08 Nov 2012 13:18

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

lgm42
Posts: 4
Joined: Thu 10 Mar 2011 08:28

Re: Invalidcast exception on FirstOrDefault command

Post by lgm42 » Thu 08 Nov 2012 13:20

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.

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Invalidcast exception on FirstOrDefault command

Post by MariiaI » Thu 08 Nov 2012 14:42

There is a known issue with InvalidCastException. We are working on it now and will inform you when it is fixed.

lgm42
Posts: 4
Joined: Thu 10 Mar 2011 08:28

Re: Invalidcast exception on FirstOrDefault command

Post by lgm42 » Fri 09 Nov 2012 09:47

For the moment waht is the best way to prevent the error ?
Because making a ToList() is very poor in performance.

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Invalidcast exception on FirstOrDefault command

Post by MariiaI » Mon 12 Nov 2012 10:23

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.

MariiaI
Devart Team
Posts: 1472
Joined: Mon 13 Feb 2012 08:17

Re: Invalidcast exception on FirstOrDefault command

Post by MariiaI » Fri 30 Nov 2012 07:55

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

Post Reply