Home
Help
Register
Log in

Search

 
   Active Threads  

You are here: Home > LLBLGen Pro > LLBLGen Pro Runtime Framework> PredicateBuilder ArgumentException
 

Pages: 1
LLBLGen Pro Runtime Framework
PredicateBuilder ArgumentException
Page:1/1 

  Print all messages in this thread  
Poster Message
TomDog
User



Location:
Wellington, New Zealand
Joined on:
25-Oct-2005 22:21:17
Posted:
570 posts
# Posted on: 05-Dec-2016 00:39:03.  
This in LinqPad against Northwind:
Code:
var predicate = PredicateBuilder.Null<EmployeeEntity>();
predicate = predicate.Or(c => c.BirthDate < DateTime.Now);
predicate = predicate.Or(c => c.HireDate == null);
var q = Employee.Where(predicate);
q.Dump();
throws "Argument types do not match"ArgumentException
Code:
at System.Linq.Expressions.Expression.Constant(Object value, Type type)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.InMemoryEvalCandidateFinder.Wrap(Expression toWrap)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.InMemoryEvalCandidateFinder.HandleExpression(Expression expressionToHandle)
at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleBinaryExpression(BinaryExpression expressionToHandle)
...
swap the order and it works fine
Code:
var predicate = PredicateBuilder.Null<EmployeeEntity>();
predicate = predicate.Or(c => c.HireDate == null);
predicate = predicate.Or(c => c.BirthDate < DateTime.Now);
var q = Employee.Where(predicate);
q.Dump();


V5.0.7
Jeremy Thomas
VS 2017 C#, LLBLGen v5.4, Winforms, WPF and ASP.NET MVCLLBL & LinqPad
 
Top
daelmo
Support Team



Location:
Guatemala City
Joined on:
28-Nov-2005 23:35:24
Posted:
8150 posts
# Posted on: 05-Dec-2016 08:19:33.  
Reproduced with RTL v5.0.8.

Works
Code:
TestMethod]
public void PredicateBuilderOrder()
{
    var predicate = PredicateBuilder.Null<EmployeeEntity>();
    predicate = predicate.Or(c => c.HireDate == null);
    predicate = predicate.Or(c => c.BirthDate < DateTime.Now);
                
            
    using (var adapter = new DataAccessAdapter())
    {
        var metaData = new LinqMetaData(adapter);
        var q = metaData.Employee.Where(predicate);

        var results = q.ToList();
    }            
}

Expression in the trace log:
Code:
value(SD.LLBLGen.Pro.LinqSupportClasses.DataSource2`1[NW.LLBL.MSSQL.Adapter.v50.EntityClasses.EmployeeEntity]).Where(c => ((c.HireDate == null) OrElse (Convert(c.BirthDate) < Convert(Convert(DateTime.Now)))))


Fails
Code:
TestMethod]
public void PredicateBuilderOrder()
{
    var predicate = PredicateBuilder.Null<EmployeeEntity>();
    predicate = predicate.Or(c => c.BirthDate < DateTime.Now);    
    predicate = predicate.Or(c => c.HireDate == null);
                                
    using (var adapter = new DataAccessAdapter())
    {
        var metaData = new LinqMetaData(adapter);
        var q = metaData.Employee.Where(predicate);

        var results = q.ToList();
    }            
}

Expression in the trace log:
Code:
value(SD.LLBLGen.Pro.LinqSupportClasses.DataSource2`1[NW.LLBL.MSSQL.Adapter.v50.EntityClasses.EmployeeEntity]).Where(c => ((c.BirthDate < Convert(DateTime.Now)) OrElse (Convert(c.HireDate) == Convert(null))))


The difference and the cause of error seems to be in Convert(null) . We will look into it.


David Elizondo
LLBLGen'ing (articles and code snippets) | linkedin | twitter
 
Top
Otis
LLBLGen Pro Team



Location:
The Hague, The Netherlands
Joined on:
17-Aug-2003 18:00:36
Posted:
38090 posts
# Posted on: 05-Dec-2016 11:51:49.  
Strange phenomenon Regular Smiley The Convert(null) is apparently inserted through predicate builder although it has no code doing so. A normal linq query doesn't insert this... Looking into a fix..
Frans Bouma
LLBLGen Pro / ORM Profiler Lead Developer | Blog | Twitter
 
Top
Otis
LLBLGen Pro Team



Location:
The Hague, The Netherlands
Joined on:
17-Aug-2003 18:00:36
Posted:
38090 posts
# Posted on: 05-Dec-2016 11:58:22.  
Casting the null to (DateTime?) works:

Code:
var predicate = PredicateBuilder.Null<EmployeeEntity>();
predicate = predicate.Or(c => c.BirthDate < DateTime.Now);
predicate = predicate.Or(c => c.HireDate == (DateTime?)null);


Does that work for you? Or is this complicated to do as you generate these expressions in a toolkit? The problem is caused by the ConvertExpression, which doesn't have the type of the other side as the type, but simply object as its value is 'null'. Casting will solve it as it will wrap the Convert(null) into a Convert() to DateTime? which will match the type of the other side.

Problem with predicatebuilder is... there's almost no code, so I don't know what exactly causes this...


Frans Bouma
LLBLGen Pro / ORM Profiler Lead Developer | Blog | Twitter
 
Top
TomDog
User



Location:
Wellington, New Zealand
Joined on:
25-Oct-2005 22:21:17
Posted:
570 posts
# Posted on: 07-Dec-2016 10:23:06.  
Yep that works for me, though actually since I made the post my real code has evolved to this
Code:

     var occPredicate = PredicateBuilder.Null<T>();
     var predicateNullDates = PredicateBuilder.Null<T>();

...

         if (!dateFrom.IsNullOrEmpty())
            occPredicate = occPredicate.And(oc => oc.DateNotifiedToAuthority >= dateFrom);
         if (!dateTo.IsNullOrEmpty())
         {
            var dateTotransformedForInclusiveSearch = dateTo.TransformForInclusiveToSearch();
            occPredicate = occPredicate.And(oc => oc.DateNotifiedToAuthority < dateTotransformedForInclusiveSearch);
         }
         if (includeNullDates)
            predicateNullDates = predicateNullDates.And(oc => oc.DateNotifiedToAuthority == null);
         return occPredicate == null ? occurrenceQuery : occurrenceQuery.Where(predicateNullDates.Or(occPredicate));
With the two PredicateBuilders it doesn't require the cast.
The intent of the code is: if at least one date filter is specified as well as the option to include null dates then OR the date range with NULL.

So all good, but still weird that it fails in my first example
Jeremy Thomas
VS 2017 C#, LLBLGen v5.4, Winforms, WPF and ASP.NET MVCLLBL & LinqPad
 
Top
Otis
LLBLGen Pro Team



Location:
The Hague, The Netherlands
Joined on:
17-Aug-2003 18:00:36
Posted:
38090 posts
# Posted on: 07-Dec-2016 10:50:49.  
Please let us know if you run into a situation where these can't be worked around. I've also added a workitem to look into this in 5.2 as it might be we can add some code to the linq provider to intercept the Convert(null) and deal with it.

Frans Bouma
LLBLGen Pro / ORM Profiler Lead Developer | Blog | Twitter
 
Top
Pages: 1  


Powered by HnD ©2002-2007 Solutions Design
HnD uses LLBLGen Pro

Version: 2.1.12172008 Final.