Question

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;
Était-ce utile?

La solution

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.

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top