- Home
- LLBLGen Pro
- LLBLGen Pro Runtime Framework
?? operator
Joined: 28-Apr-2009
I have similar issue mentioned at
http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=18955
Is above bug resolved in SD.LLBLGen.Pro.LinqSupportClasses.NET35.dll v2.6.10.1119? (this is the version i am using)
In my case it generates wrong sql:
I am facing issue with below code i expects that when deleteState is null generated where should be like
[LPA_L1].[IsDeleted] =[LPA_L1].[IsDeleted]
not
[LPA_L1].[IsDeleted] IS NULL
.
entity UserRoleOnCountryInStudy is inherited from UserRoleOnCountryInStudy that is why it is there in generated query.
Linq Query
bool? deleteState = null;
var userRoleOnCountryInStudyEntities = linqAdapter.UserRoleOnCountryInStudy
.Where(p => p.CountryInStudyGuid == countryInStudyGuid && p.IsDeleted == (deleteState ?? p.IsDeleted)).Select(p => p);
SQL
exec sp_executesql N'
SELECT DISTINCT [LPA_L1].[UserRoleOnClinicalDataGUID] AS [F3_0], [LPA_L1].[UserRoleGUID] AS [F3_1],[LPA_L1].[IsDeleted] AS [F3_3], [LPA_L2].[UserRoleOnCountryInStudyGUID] AS [F4_8], [LPA_L2].[CountryInStudyGUID] AS [F4_9]
FROM ( [TestDB].[dbo].[UserRoleOnClinicalData] [LPA_L1]
INNER JOIN [TestDB].[dbo].[UserRoleOnCountryInStudy] [LPA_L2] ON [LPA_L1].[UserRoleOnClinicalDataGUID]=[LPA_L2].[UserRoleOnCountryInStudyGUID])
WHERE ( ( ( ( ( [LPA_L2].[CountryInStudyGUID] = @CountryInStudyGuid1) AND ( [LPA_L1].[IsDeleted] IS NULL)))) AND ( [LPA_L2].[UserRoleOnCountryInStudyGUID] IS NOT NULL))
',N'@CountryInStudyGuid1 uniqueidentifier',@CountryInStudyGuid1='F8CD138A-CF7B-4137-9B86-DB91C950274B'
This query i have created for test purpose from a very complex query so adding conditional where is not an immediate solution for me.
morbia wrote:
I have similar issue mentioned at
http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=18955
Is above bug resolved in SD.LLBLGen.Pro.LinqSupportClasses.NET35.dll v2.6.10.1119? (this is the version i am using)
Not sure if that is the same. As that scenario is (t.field ?? t.fieldMapped.field), and your scenario is (scalar ?? field).
morbia wrote:
I am facing issue with below code i expects that when deleteState is null generated where should be like
[LPA_L1].[IsDeleted] =[LPA_L1].[IsDeleted]
not
[LPA_L1].[IsDeleted] IS NULL
.
entity UserRoleOnCountryInStudy is inherited from UserRoleOnCountryInStudy that is why it is there in generated query.
Linq Query
bool? deleteState = null; var userRoleOnCountryInStudyEntities = linqAdapter.UserRoleOnCountryInStudy .Where(p => p.CountryInStudyGuid == countryInStudyGuid && p.IsDeleted == (deleteState ?? p.IsDeleted)).Select(p => p);
SQL
exec sp_executesql N' SELECT DISTINCT [LPA_L1].[UserRoleOnClinicalDataGUID] AS [F3_0], [LPA_L1].[UserRoleGUID] AS [F3_1],[LPA_L1].[IsDeleted] AS [F3_3], [LPA_L2].[UserRoleOnCountryInStudyGUID] AS [F4_8], [LPA_L2].[CountryInStudyGUID] AS [F4_9] FROM ( [TestDB].[dbo].[UserRoleOnClinicalData] [LPA_L1] INNER JOIN [TestDB].[dbo].[UserRoleOnCountryInStudy] [LPA_L2] ON [LPA_L1].[UserRoleOnClinicalDataGUID]=[LPA_L2].[UserRoleOnCountryInStudyGUID]) WHERE ( ( ( ( ( [LPA_L2].[CountryInStudyGUID] = @CountryInStudyGuid1) AND ( [LPA_L1].[IsDeleted] IS NULL)))) AND ( [LPA_L2].[UserRoleOnCountryInStudyGUID] IS NOT NULL)) ',N'@CountryInStudyGuid1 uniqueidentifier',@CountryInStudyGuid1='F8CD138A-CF7B-4137-9B86-DB91C950274B'
I have to check as you are mixing an in-memory variable and a field, so I think the expression (_p.IsDeleted == (deleteState ?? p.IsDeleted)_) is evaluated in-memory.
So, want you want to achieve is equivalent to this: code]
var userRoleOnCountryInStudyEntities = linqAdapter.UserRoleOnCountryInStudy;
if (deleteState != null)
{
userRoleOnCountryInStudyEntities
.Where(p => p.CountryInStudyGuid == countryInStudyGuid && p.IsDeleted == deleteState)
.Select(p => p);
}
else
{
userRoleOnCountryInStudyEntities
.Where(p => p.CountryInStudyGuid == countryInStudyGuid)
.Select(p => p);
}
Joined: 28-Apr-2009
morbia wrote:
So, want you want to achieve is equivalent to this: code]
var userRoleOnCountryInStudyEntities = linqAdapter.UserRoleOnCountryInStudy;
if (deleteState != null) { userRoleOnCountryInStudyEntities .Where(p => p.CountryInStudyGuid == countryInStudyGuid && p.IsDeleted == deleteState) .Select(p => p); } else { userRoleOnCountryInStudyEntities .Where(p => p.CountryInStudyGuid == countryInStudyGuid) .Select(p => p); }
Yes above is correct, that is what i want to achieve, but i don't have possibility of using if else, above query is a small part of complex query, i have many SubPath, WithPath and PrefetchPath and each entity have soft delete so filter for IsDelete is there on each entity involved.
Do you see any possibility for support of filter (scalar ?? field)?
Does the following work?
bool? deleteState = null;
var userRoleOnCountryInStudyEntities = linqAdapter.UserRoleOnCountryInStudy
.Where(p => p.CountryInStudyGuid == countryInStudyGuid && p.IsDeleted == ((deleteState == null)? p.IsDeleted : deleteState)).Select(p => p);
Joined: 28-Apr-2009
Walaa wrote:
Does the following work?
bool? deleteState = null; var userRoleOnCountryInStudyEntities = linqAdapter.UserRoleOnCountryInStudy .Where(p => p.CountryInStudyGuid == countryInStudyGuid && p.IsDeleted == ((deleteState == null)? p.IsDeleted : deleteState)).Select(p => p);
It worked thank you, and below is generated SQL:
( [LPA_L2].[IsDeleted] = CASE WHEN @LO02=1 THEN [LPA_L2].[IsDeleted] ELSE @LO03 END))
but below query failed, position of scalar value is making difference
p.IsDeleted == ((deleteState.HasValue)? deleteState : p.IsDeleted )
OR
p.IsDeleted == ((deleteState != null)? deleteState : p.IsDeleted )
here is stack trace
Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at SD.LLBLGen.Pro.LinqSupportClasses.LinqUtils.ObtainRealValueFromConstantWrapped(MemberExpression expressionToHandle, Expression memberContainer)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleMemberIsPartOfConstant(MemberExpression expressionToHandle, Expression memberContainer)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleMemberExpression(MemberExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleInMemoryEvalCandidateExpression(InMemoryEvalCandidateExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleConditionalExpression(ConditionalExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleBinaryExpression(BinaryExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleBinaryExpression(BinaryExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleBinaryExpressionBooleanOperator(BinaryExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleBinaryExpression(BinaryExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleBinaryExpression(BinaryExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleBinaryExpressionBooleanOperator(BinaryExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleBinaryExpression(BinaryExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleBinaryExpression(BinaryExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleBinaryExpressionBooleanOperator(BinaryExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleBinaryExpression(BinaryExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleBinaryExpression(BinaryExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleBinaryExpressionBooleanOperator(BinaryExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleLambdaExpression(LambdaExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleMethodCallWhere(MethodCallExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleQueryableExtensionMethod(MethodCallExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleMethodCallPerType(MethodCallExpression expressionToHandle, Type declaringType)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleMethodCallExpression(MethodCallExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleMethodCallSelect(MethodCallExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleQueryableExtensionMethod(MethodCallExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleMethodCallPerType(MethodCallExpression expressionToHandle, Type declaringType)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleMethodCallExpression(MethodCallExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleMethodCallWithPath(MethodCallExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleQueryableExtensionMethod(MethodCallExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleMethodCallPerType(MethodCallExpression expressionToHandle, Type declaringType)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleMethodCallExpression(MethodCallExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.PreProcessor.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.HandleExpressionTree(Expression expression)
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.Execute(Expression expression)
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.System.Linq.IQueryProvider.Execute(Expression expression)
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery`1.Execute()
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
Hi morbia,
It seems the order of the parameters (scalar and entity field) matters in this. We will look into this. In the meantime, please use the workaround posted by Walaa.
((deleteState == null)? p.IsDeleted : deleteState)
Joined: 28-Apr-2009
daelmo wrote:
Hi morbia,
It seems the order of the parameters (scalar and entity field) matters in this. We will look into this. In the meantime, please use the workaround posted by Walaa.
((deleteState == null)? p.IsDeleted : deleteState)
I found other issue, above solution works only in case of filter in where, if I use this filter on Prefetch it throws below error:
Exception: System.InvalidOperationException: The binary operator Equal is not defined for the types 'System.Boolean' and 'System.Object'.
at System.Linq.Expressions.Expression.GetEqualityComparisonOperator(ExpressionType binaryType, String opName, Expression left, Expression right, Boolean liftToNull)
at System.Linq.Expressions.Expression.Equal(Expression left, Expression right, Boolean liftToNull, MethodInfo method)
at System.Linq.Expressions.Expression.Equal(Expression left, Expression right)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.ProduceBooleanResultValueFromBinaryInMemoryExpression(Object leftOperand, Object rightOperand, ComparisonOperator operatorToUse)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleBinaryExpressionSeparateOperands(BinaryExpression expressionToHandle, Expression leftSide, Expression rightSide)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleBinaryExpressionBooleanOperator(BinaryExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleDbFunctionCallExpression(DbFunctionCallExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleBinaryExpressionBooleanOperator(BinaryExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleLambdaExpression(LambdaExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandlePathEdgeExpression(PathEdgeExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandlePrefetchPathEdges(ReadOnlyCollection`1 edgeExpressions)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandlePrefetchPathExpression(PrefetchPathExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleSelectExpression(SelectExpression expressionToHandle, SelectExpression newInstance)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleSelectExpression(SelectExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleSelectExpression(SelectExpression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.HandleExpressionTree(Expression expression)
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.Execute(Expression expression)
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.System.Linq.IQueryProvider.Execute(Expression expression)
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery`1.Execute()
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
The query:
var userRoleOnCountryInStudyEntities = linqAdapter.UserRoleOnCountryInStudy
.Where(p => p.CountryInStudyGuid == countryInStudyGuid
&& p.IsDeleted == (deleteState == null ? p.IsDeleted : deleteState)
&& p.UserRole.IsDeleted == (deleteState == null ? p.UserRole.IsDeleted : deleteState)
&& p.UserRole.User.IsDeleted == (deleteState == null ? p.UserRole.User.IsDeleted : deleteState))
.Select(p => p).WithPath(csp => csp.Prefetch<ContactEntity>(cp => cp.Contact)
.FilterOn(f => f.IsDeleted == (deleteState == null ? f.IsDeleted : deleteState)));
above query without WithPath works fine.