Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Query() using Linq doesn't work, but similar generated BsonExpression does #1636

Closed
deli-nik opened this issue Apr 17, 2020 · 1 comment

Comments

@deli-nik
Copy link

deli-nik commented Apr 17, 2020

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.
image

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?

@lbnascimento
Copy link
Collaborator

@adli89 This issue has been fixed in the latest master and its fix will be present in the next incremental release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants