Page 1 of 1

Linq error

Posted: Tue 26 Jul 2011 15:45
by PatrickG
I am having some trouble when generating a linq query against the results of a prior linq query.

The exception that I get is:
System.InvalidCastException was unhandled
Message=Unable to cast object of type 'Devart.Data.Linq.Provider.ProviderType' to type 'Devart.Data.PostgreSql.Linq.Provider.PgSqlProviderType'.
Source=Devart.Data.PostgreSql.Linq
StackTrace:
at Devart.Data.PostgreSql.Linq.Provider.Query.a.a(ProviderType A_0, ProviderType A_1)
at Devart.Data.Linq.Provider.Query.DbMethodCallConverter.TranslateToServerMethod(List`1 serverMethods, List`1 largeProviderTypes, List`1 arguments, Type returnType)
at Devart.Data.Linq.Provider.Query.DbMethodCallConverter.VisitMethodCall(bd mc)
at Devart.Data.Linq.Provider.Query.SqlVisitor.Visit(SqlNode node)
at Devart.Data.Linq.Provider.Query.SqlVisitor.VisitExpression(SqlExpression exp)
at Devart.Data.Linq.Provider.Query.SqlVisitor.VisitBinaryOperator(av bo)
at Devart.Data.Linq.Provider.Query.DbMethodCallConverter.VisitBinaryOperator(av bo)
at Devart.Data.Linq.Provider.Query.SqlVisitor.Visit(SqlNode node)
at Devart.Data.Linq.Provider.Query.SqlVisitor.VisitExpression(SqlExpression exp)
at Devart.Data.Linq.Provider.Query.SqlVisitor.VisitBinaryOperator(av bo)
at Devart.Data.Linq.Provider.Query.DbMethodCallConverter.VisitBinaryOperator(av bo)
at Devart.Data.Linq.Provider.Query.SqlVisitor.Visit(SqlNode node)
at Devart.Data.Linq.Provider.Query.SqlVisitor.VisitExpression(SqlExpression exp)
at Devart.Data.Linq.Provider.Query.SqlVisitor.VisitSelectCore(SqlSelect select)
at Devart.Data.Linq.Provider.Query.DbMethodCallConverter.VisitSelect(SqlSelect select)
at Devart.Data.Linq.Provider.Query.SqlVisitor.VisitAlias(e a)
at Devart.Data.Linq.Provider.Query.DbMethodCallConverter.VisitAlias(e a)
at Devart.Data.Linq.Provider.Query.SqlVisitor.Visit(SqlNode node)
at Devart.Data.Linq.Provider.Query.SqlVisitor.VisitSource(SqlNode source)
at Devart.Data.Linq.Provider.Query.SqlVisitor.VisitSelectCore(SqlSelect select)
at Devart.Data.Linq.Provider.Query.DbMethodCallConverter.VisitSelect(SqlSelect select)
at Devart.Data.Linq.Provider.Query.SqlVisitor.VisitAlias(e a)
at Devart.Data.Linq.Provider.Query.DbMethodCallConverter.VisitAlias(e a)
at Devart.Data.Linq.Provider.Query.SqlVisitor.Visit(SqlNode node)
at Devart.Data.Linq.Provider.Query.SqlVisitor.VisitSequence(SqlSelect sel)
at Devart.Data.Linq.Provider.Query.DbMethodCallConverter.VisitScalarSubSelect(ad ss)
at Devart.Data.Linq.Provider.Query.SqlVisitor.VisitSubSelect(ad ss)
at Devart.Data.Linq.Provider.Query.SqlVisitor.Visit(SqlNode node)
at Devart.Data.Linq.Provider.Query.SqlVisitor.VisitExpression(SqlExpression exp)
at Devart.Data.Linq.Provider.Query.SqlVisitor.VisitRow(af row)
at Devart.Data.Linq.Provider.Query.SqlVisitor.Visit(SqlNode node)
at Devart.Data.Linq.Provider.Query.SqlVisitor.VisitSelectCore(SqlSelect select)
at Devart.Data.Linq.Provider.Query.DbMethodCallConverter.VisitSelect(SqlSelect select)
at Devart.Data.Linq.Provider.Query.SqlVisitor.VisitAlias(e a)
at Devart.Data.Linq.Provider.Query.DbMethodCallConverter.VisitAlias(e a)
at Devart.Data.Linq.Provider.Query.SqlVisitor.Visit(SqlNode node)
at Devart.Data.Linq.Provider.DataProvider.BuildQuery(ResultShape resultShape, Type resultType, SqlNode node, IList`1 externalParameterAccessors)
at Devart.Data.Linq.Provider.DataProvider.BuildQuery(Expression query)
at Devart.Data.Linq.Provider.DataProvider.Devart.Data.Linq.Provider.IProvider.Execute(Expression query)
at Devart.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
at System.Linq.Queryable.Count[TSource](IQueryable`1 source)

Is there any insight you can provide to help be diagnose this problem?

Patrick

Posted: Wed 27 Jul 2011 12:39
by StanislavK
Could you please describe the issue in more details? For example, please specify the definitions of the tables you are querying, and the queries (both initial and secondary) you are trying to perform.

Posted: Wed 27 Jul 2011 23:35
by PatrickG
Here is a snippet of the code and and observation I was able to make...

Code: Select all

var baseQuery = from c in this.vwConfiguration
		where c.ConfigurationType.Equals(configurationType) && c.IsExpired.Equals(0)
		select c;

baseQuery = from c in baseQuery where c.SiteUID == null select c;

var q = from c in baseQuery
	where ((string.IsNullOrEmpty(product) && c.Product == null) || c.Product.Equals(product)) &&
	c.SiteUID.Equals(null) &&
	((string.IsNullOrEmpty(application) && c.Application == null) || c.Application.Equals(application))
	select c;
At this point q.Count or q.SingleOrDefault will throw the previously posted exception.


Interestingly enough if I comment out the line that checks to see if c.SiteUID is null, the query executes fine.

Code: Select all

var q = from c in baseQuery
	where ((string.IsNullOrEmpty(product) && c.Product == null) || c.Product.Equals(product)) &&
	//c.SiteUID.Equals(null) &&
	((string.IsNullOrEmpty(application) && c.Application == null) || c.Application.Equals(application))
	select c;
Note that SiteUID is of type System.Nullable

Posted: Thu 28 Jul 2011 15:30
by StanislavK
Thank you for the report, we have reproduced the problem. We will analyze it and inform you about the results.

As a workaround, you can try using the '==' operator instead of .Equals in the problem line.

Posted: Fri 29 Jul 2011 18:26
by PatrickG
This does fix my problem, thank you.

It seems that this might have been a change from .Net 3.5 to .Net 4 as the same code fails in LinqToObjects and it used to work, at least in LinqToSql. Of course I converted my project to .Net 4 at the same time I am trying to convert to PostgreSQL and the invalid cast exception didn't seem to indicate that a null value was the problem...

Thanks again,

Patrick

Posted: Mon 01 Aug 2011 13:10
by StanislavK
We've reproduced this problem with both 3.5 and 4.0 Framework. We will post here when any new information about the issue is available.

Posted: Fri 07 Oct 2011 09:49
by StanislavK
We have fixed the problem with the '.Equals(null)' translation. The fix is available in the latest 3.0.5 build of LinqConnect, which can be downloaded from
http://www.devart.com/linqconnect/download.html
(the trial only) or from Registered Users' Area (for users with active subscription only).

For more information about the fixes and improvements available in LinqConnect 3.0.5, please refer to
http://www.devart.com/forums/viewtopic.php?t=22168

Posted: Fri 07 Oct 2011 13:19
by cjbiggs
So is this fix in dotConnect for PostgreSQL as well or just LinqConnect?

Thanks,

Charlie J.

Posted: Mon 10 Oct 2011 14:57
by StanislavK
Yes, it is. The trial of the new dotConnect for PostgreSQL build can be downloaded from
http://www.devart.com/dotconnect/postgr ... nload.html
(other editions are available in Registered Users' Area). For the details about the fixes and improvements, please refer to
http://www.devart.com/forums/viewtopic.php?t=22166