Closed xumix closed 9 years ago
Fixed in 71ffd11fed78a157006f1b67e3cc5c8978930431.
Great, thanks!
Released in v3.5.3
Well, there is opposite situation now:
query = query.Where(q => q.SubjectId.HasValue && parameter.Programs.Contains(q.SubjectId.Value));
System.InvalidCastException occurred
HResult=-2147467262
Message=Unable to cast object of type 'System.Linq.Expressions.UnaryExpression' to type 'System.Linq.Expressions.ConstantExpression'.
Source=Lucene.Net.Linq
StackTrace:
at Lucene.Net.Linq.Transformation.TreeVisitors.SubQueryContainsTreeVisitor.VisitSubQueryExpression(SubQueryExpression expression)
at Remotion.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression)
at Remotion.Linq.Parsing.ExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression)
at Remotion.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression)
at Remotion.Linq.Clauses.WhereClause.TransformExpressions(Func`2 transformation)
at Lucene.Net.Linq.Transformation.QueryModelTransformer.VisitWhereClause(WhereClause whereClause, QueryModel queryModel, Int32 index)
at Remotion.Linq.Clauses.WhereClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel, Int32 index)
at Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection`1 bodyClauses, QueryModel queryModel)
at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
at Remotion.Linq.QueryModel.Accept(IQueryModelVisitor visitor)
at Lucene.Net.Linq.LuceneQueryExecutorBase`1.PrepareQuery(QueryModel queryModel)
at Lucene.Net.Linq.LuceneQueryExecutorBase`1.<ExecuteCollection>d__3`1.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at RestChild.Booking.Logic.TourSearchService.SearchTours(SearchToursParams parameter) in c:\Projects\RestChild\RestChild.Booking.Logic\TourSearchService.cs:line 90
InnerException:
This would be a separate feature to support this. Supposing that parameter.Programs
is a set of int (with example values 3, 7, 10), in effect you are trying to execute a query like:
SubjectId:3 OR SubjectId:7 OR SubjectId:10
Lucene does not provide a way to do something like SubjectId:[3 OR 7 OR 10]
.
Since there is no native support for this type of query, Lucene.Net.Linq does not try to bolt one on.
As an alternative, you can construct your own BooleanQuery and use the IQueryable<T>.Where(Query)
extension method provided by Lucene.Net.Linq.
This is rather strange, why not translate to this exact form?
SubjectId:3 OR SubjectId:7 OR SubjectId:10
Looks like a simple and proper solution since the collection is finite
It might be intuitive, but that doesn't make it trivial. Of course pull requests are always welcomed.
I've used this project https://www.nuget.org/packages/PredicateExtensions/1.0.0 to make it work like this:
if (parameter.Programs.NotNullOrEmpty())
{
Expression<Func<IndexTourDto, bool>> pr = a => true;
foreach (var program in parameter.Programs)
{
var tmp = program;
pr = pr.Or(p => p.SubjectId == tmp);
}
query = query.Where(pr);
}
But it looks ugly :(
The model looks like
the query:
The error: