Invalid translation of EndsWithPredicate

I’m writing generic queries using the PredicateBuilder.

I’m starting with an IMongoQueryable<T> and append a where condition

items = items.Where(PredicateBuilder.StartsWithPredicate<T>(nameof(INamedItem.Name), "categorys"));
items = items.Where(PredicateBuilder.EndsWithWithPredicate<T>(nameof(INamedItem.Name), "categorys"));

Both these lines generate the same syntax:

{ "$match" : { "Name" : /^category/s } }

I’d argue that EndsWith should use another syntax.

Hi, @Stephan_Steiner,

PredicateBuilder appears to be a class from LinqKit and is not part of the MongoDB .NET/C# Driver. PredicateBuilder appears to be appending AST nodes to a LINQ query. The .NET/C# Driver’s LINQ implementation will translate those AST nodes into MQL whether you write those AST nodes in C# (e.g. Where(expression)) or via some library like LinqKit. Thus my suspicion is that PredicateBuilder.EndsWithPredicate<T> is not behaving as desired. This could be due to a bug in LinqKit or incorrect use of the PredicateBuilder API.

Hope that helps to direct your investigations as to the root cause of the problem.


Urgh, I was under the impression PredicateBuilder was a standard class. Turns out you’re right, it’s an external class. In fact, we have our own PredicateBuilder in our ow tools lib, and it is the cause of the problem - it uses StartsWith instead of EndsWith.

Sorry for the confusion - I’m now off to fix our library…

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.