question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using PredicateBuilder return NotSupportedException [BUG]

See original GitHub issue

Version 5.0.9 Code to Reproduce For simplicity:

  • I use a very simple query to describe the issue
  • I’m using LINQPad,
void Main()
{
	var cs = new ConnectionString
	{
		Filename = @"c:\mydb.db",
		Password = @"MyP@55w0rd"
	};
	using (var db = new LiteDatabase(cs))
	{
		var col = db.GetCollection<Product>("Prods");
		col.Insert(new Product{ Name="AAA"});
		col.Insert(new Product{ Name="AEE"});

		var predicate = PredicateBuilder.False<Product>();

		predicate = predicate.Or(p => p.Name.Contains("A"));
		col.Find(predicate).Count().Dump(); //<== NotSupportedException 
	}
}
public class Product
{
	public int Id{get;set;}
	public string Name{get;set;}
}

Expected behavior The Count method should return the number of documents

Stacktrace

at LiteDB.LinqExpressionVisitor.Resolve(Boolean predicate)   
at LiteDB.BsonMapper.GetExpression[T,K](Expression`1 predicate)   
at LiteDB.LiteCollection`1.Find(Expression`1 predicate, Int32 skip, Int32 limit)   
at UserQuery.Main(), line 20

Exception Message

Invalid BsonExpression when converted from Linq expression: f => (False OrElse Invoke(p => p.Name.Contains("A"), f)) - `(((@p0) = true) OR @.Name LIKE ('%' + @p1 + '%')$)`

LiteException

Unexpected token `$` in position 49. 

Any help would be greatly appreciated.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
lbnascimentocommented, Sep 10, 2020

@toumir This issue has been fixed in the master and its fix will be present in the next incremental release. In the meantime, I suggest you use the ILiteQueryable<T> interface from col.Query(), for which I gave an example in my previous comment.

0reactions
djarviscommented, Oct 15, 2020

I’m also looking for a fix for this. I have so very much code using PredicateBuilder that I really don’t want to rewrite, plus I am unsure how I can substitute col.Query() for every single instance (queries with dynamic and complex and’s and or’s, using predicates with DeleteMany(), etc.).

@lbnascimento Am I missing some documentation page on how to use Query()? I can’t find anything.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stackoverflow Exception using PredicateBuilder with Entity ...
I have added my own Expression builder engine, i.e. much better way to generate the Predicate. PredicateBuilder works well with LINQ to Object, ......
Read more >
PROBLEM with LINQKit pluggable expressions on OData ...
When I switched back to the VB version of the query, it works as expected (i got the server-side filtering OData query) when...
Read more >
Combine predicates with PredicateBuilder
A predicate is a function that returns true or false for an item it evaluates. A query Where clause takes a predicate that...
Read more >
A universal PredicateBuilder | Monty's Gush - WordPress.com
I don't use the famous Albahari PredicateBuilder because it doesn't work so well with Entity Framework or the latest version of NHibernate.
Read more >
Create dynamic Linq queries with Predicate Builder
Create dynamic Linq queries with Predicate Builder ... This is a delegate who acts on the given argument, but does not return anything....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found