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.

[QUESTION] Predicate Order matter ?

See original GitHub issue

I wonder if the order is matter when querying the database ? The results of the following two queries are different, can someone explain that to me,

LiteDB version: master branch

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.DeleteAll();
		
		#region
		col.Insert(new Product { Id = 4, Name = "Vantage" });
		col.Insert(new Product { Id = 5, Name = "Corvette" });
		col.Insert(new Product { Id = 7, Name = "Caprice" });
		col.Insert(new Product { Id = 6, Name = "3000GT" });
		#endregion
		
		col.EnsureIndex(c => c.Name);
		
		Expression<Func<Product, bool>> p1 = p=>false; 
		Expression<Func<Product, bool>> p2 = p=>p.Name.Contains("a");
		
		//1 query
		col
		.Query()
		.Where(p2.Or(p1))
		.Count();//<== cout == 2
		
		//2 query
		col
		.Query()
		.Where(p1.Or(p2))
		.Count();//<== cout == 0
	}
}
// Define other methods, classes and namespaces here
public class Product
{
	public int Id{get;set;}
	public string Name{get;set;}
}

Thank you in advance

_Originally posted by @toumir in https://github.com/mbdavid/LiteDB/issues/1824#issuecomment-697361182_

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
AlBannaTechnocommented, Oct 20, 2020

this workaround worked for me

 public IList<Expression<Func<Product, bool>>> GetListPredicates()
{
var lstExpr = new List<Expression<Func<Product,bool>>> {p => p.IsChrgDechrg};
if(condition)
{
	lstExpr.Add(p => p.IsSynch);
}
if(condition2)
{
	lstExpr.Add(p => p.Name.Constains("a"));
}
....
 return lstExpr;
}

usage

var queryable = Context.GetCollection<T>().Query();
foreach (var p in GetListPredicates())
{
    queryable.Where(p);
 });			
queryable.ToList();

@toumir

I Used similar solution for a long time and its is work perfectly, But we still need to fix the bug in the library.

So i wait, @lbnascimento, and i will try to solve it.

0reactions
toumircommented, Oct 20, 2020

@AlBannaTechno maybe @lbnascimento answer you 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Predicate Logic - Does order matter with Universal or ...
1. the answer to your title is that yes, it matters. · "For every man, there is a woman married to this man"...
Read more >
TSQL - Do the JOIN predicates order matter
I know there is a lot of discussion on WHERE predicates order mattering/not mattering and where to put the predicates WHERE clause vs...
Read more >
What is the most effective way to identify the optimal order ...
What is the most effective way to identify the optimal order of predicates in a WHERE clause? My background is in RDBMS and...
Read more >
WHERE clause in query - does order really matter? - Ask TOM
The question was the following:Assuming a variable @var that is an integer and has a value of 0 (zero).What is the best scenario?a)...
Read more >
Does the order of the "ON" portion of a JOIN matter? : r/SQL
Question for you: does the order matter if you have ANDs in your join statement? As an example, JOIN B ON a.id =...
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