1

Is it possible to use the PredicateBuilder class to build a WHERE clause that RavenDB can interpret and use? I've tried session.Query() and LuceneQuery, but they each failed:

Here is the session.Query() attempt:

public static List<T> GetObjectList<T>(Expression<Func<T, bool>> whereClause)
{
    using (IDocumentSession session = GetRavenSession())
    {
        return session.Query<T>().Where(whereClause).Take(int.MaxValue).ToList();
    }
}

This is the run-time error:

Lucene.Net.QueryParsers.ParseException: Cannot parse '( OR) OR': Encountered " "OR "" at line 1, column 2. Was expecting one of: (List of expected items here).

And if I try the LuceneQuery():

public static List<T> GetObjectList<T>(Expression<Func<T, bool>> whereClause)
{
    Func<T, bool> compiledWhereClause = whereClause.Compile();

    using (IDocumentSession session = GetRavenSession())
    {
        return session.Advanced.LuceneQuery<T>().Where(compiledWhereClause).Take(int.MaxValue).ToList();
    }
}

I get this compile-time error:

Error 2 'Raven.Client.IDocumentQueryBase>.Where(System.Func)' is obsolete: ' You cannot issue an in memory filter - such as Where(x=>x.Name == "Ayende") - on IDocumentQuery.

Edit: This is what whereClause looks like in the first example:

{f => ((False OrElse Invoke(x => (Convert(x).EquipmentId == value(ReadFromRaven.Logic.Readers.RavenReader1+<>c__DisplayClassa[WriteToRaven.Data.Marker]).tempCoater.MarkerEquipmentId), f)) OrElse Invoke(x => (Convert(x).EquipmentId == value(ReadFromRaven.Logic.Readers.RavenReader1+<>c__DisplayClassa[WriteToRaven.Data.Marker]).tempCoater.MarkerEquipmentId), f))}

Edit 2: This is how I'm building the WHERE clause

This is the call:

List<T> newList = RavenDataAccess.GetObjectList<T>(BuildWhereClause(x => x.MarkerReadTime > timeChunk.StartTime && x.MarkerReadTime <= timeChunk.EndTime));

And this is the BuildWhereClause() method signature and the parts of the method that matter:

private static Expression<Func<T, bool>> BuildWhereClause(Expression<Func<T, bool>> readTimeExpression)

    Expression<Func<T, bool>> innerWhereClause = PredicateBuilder.False<T>();

    foreach (Coater coater in coaters)
    {
        var tempCoater = coater;
        innerWhereClause = innerWhereClause.Or<T>(x => x.EquipmentId == tempCoater.MarkerEquipmentId);
    }

    Expression<Func<T, bool>> outerWhereClause = PredicateBuilder.True<T>();
    outerWhereClause = outerWhereClause.And<T>(readTimeExpression);
    outerWhereClause = outerWhereClause.And<T>(innerWhereClause);

    _whereClause = innerWhereClause;

    return _whereClause;
Bob Horn
  • 33,387
  • 34
  • 113
  • 219
  • In the 1st attempt, what does `session.Query().Where(whereClause).Take(int.MaxValue).ToString()` show? – Matt Warren Apr 23 '12 at 21:14
  • Also what does the Expression that is being passed into the GetListObject(..) funciton look like in the debugger (`whereClause`)? Is there a chance that is has empty clauses, i.e. just contains "OR" statments and brackets? – Matt Warren Apr 23 '12 at 22:36
  • @MattWarren ToString() shows this: ( OR) OR. I added the value of whereClause to my question. Does that help at all? – Bob Horn Apr 24 '12 at 00:34

2 Answers2

1

I have same error. This is how I solve:

I use the predicateBulder from http://www.albahari.com/nutshell/predicatebuilder.aspx and called as follows: Session.Query().Where(predicate.Compile()).ToList(); Please note the I called predicate.Compile()methed.

pravangsu
  • 11
  • 1
0

What is the actual expression that you are trying to pass to the queries?

If you want to build a query dynamically, use Lucene Query, don't try to dynamically build something with linq.

Ayende Rahien
  • 22,925
  • 1
  • 36
  • 41
  • I added the code for how I'm building the where clause. And I tried using a Lucene Query, but I got a compile time error. Are you saying I should use Lucene *without* PredicateBuilder? – Bob Horn Apr 24 '12 at 11:57