You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
LiteDb Version: 5.0.7
OS: Android 9
Platform: Xamarin Forms 4.5.0.530, .NET Standard 2.1
IEnumerable<T>.Contains(T) method doesn't work when combined with || operator when doing Query().
I'm querying my database using Linq, and unexpectedly it returns 0 result (filterText is one of elements in Barcodes in my test data). When I try the same equivalent BsonExpression, I do get results. Below is my code:
var query = _database.GetCollection<Product>().Query();
query = query.Where(p => p.Barcodes.Contains(filterText)
|| p.ProductDisplayName.Contains(filterText)
|| p.Sku.Contains(filterText));
var result = query.ToList(); // This returns 0 count
var be = BsonExpression.Create("((($.Barcodes[*] ANY=@p0=true) OR ($.ProductDisplayName LIKE (\"%\"+@p1+\"%\")=true)) OR ($.Sku LIKE (\"%\"+@p2+\"%\")=true))", filterText, filterText, filterText);
var result2 = _database.GetCollection<Product>().Query().Where(be).ToList(); // Returns result
This is the (simplified) model of Product:
public class Product
{
[JsonProperty("sku")]
public string Sku { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonIgnore]
public string ProductDisplayName => Name?.RemoveHtmlAndDecode();
[JsonIgnore]
public IEnumerable<string> Barcodes { get; set; }
}
During runtime after obtaining result, I hovered over query and looked at the generated Bson Expression, which is the same as be above.
When I try querying only that condition, it returns result as expected. It only doesn't work when combined with other conditions.
var query = _database.GetCollection<Product>().Query();
var result3 = query.Where(p => p.Barcodes.Contains(filterText)).ToList(); // This returns results as expected
Also, the same query works for other conditions (string.Contains(string)). Only IEnumerable<T>.Contains(T) doesn't return as expected when combined.
In the meantime, I can use the BsonExpression for querying, but it is not preferable compared to using Linq.
Any way to make it work with Linq? Or am I doing something wrong?
The text was updated successfully, but these errors were encountered:
LiteDb Version: 5.0.7
OS: Android 9
Platform: Xamarin Forms 4.5.0.530, .NET Standard 2.1
IEnumerable<T>.Contains(T)
method doesn't work when combined with||
operator when doingQuery()
.I'm querying my database using Linq, and unexpectedly it returns 0 result (
filterText
is one of elements inBarcodes
in my test data). When I try the same equivalent BsonExpression, I do get results. Below is my code:This is the (simplified) model of
Product
:During runtime after obtaining
result
, I hovered overquery
and looked at the generated Bson Expression, which is the same asbe
above.When I try querying only that condition, it returns result as expected. It only doesn't work when combined with other conditions.
Also, the same
query
works for other conditions (string.Contains(string)
). OnlyIEnumerable<T>.Contains(T)
doesn't return as expected when combined.In the meantime, I can use the BsonExpression for querying, but it is not preferable compared to using Linq.
Any way to make it work with Linq? Or am I doing something wrong?
The text was updated successfully, but these errors were encountered: