Skip to content

No results using Linq Where() with List.Count condition #138

Closed
@ulja-dd

Description

@ulja-dd

Hi,

I'm using CouchCB.NET 3.1.0 via nuget.org

The db query is supposed to return all User documents where the List property RoleIds is not empty. I'm using the following Linq statement to do so:

var usersWithRoles = await context.Users
   .Where(u => u.RoleIds.Count > 0)
   .Take(int.MaxValue)
   .ToListAsync();

The expected result is to get some users. The Users database has users where the condition is true, however the usersWithRoles is always empty.

Is there something I'm doing wrong or is this a bug?

To replicate this issue, you may use the following programm:

using CouchDB.Driver;
using CouchDB.Driver.Extensions;
using CouchDB.Driver.Options;
using CouchDB.Driver.Types;
using Flurl.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CountIssue
{
    class Program
    {
        static async Task Main(string[] args)
        {
            await using var context = new Context();

            await InsertUsersAsync(context);

            await CountQueryAsync(context);
        }

        private static async Task CountQueryAsync(Context context)
        {
            var usersWithRoles = await context.Users
                .Where(u => u.RoleIds.Count > 0)
                .Take(int.MaxValue)
                .ToListAsync();

            Console.WriteLine($"\n\nusersWithRoles: {usersWithRoles.Count}");
        }

        private static async Task InsertUsersAsync(Context context)
        {
            var user1 = new User()
            {
                Id = Guid.NewGuid().ToString(),
                Name = "user 1",
                RoleIds = new List<string>()
                {
                    "r1"
                }
            };
            var user2 = new User()
            {
                Id = Guid.NewGuid().ToString(),
                Name = "user 2",
                RoleIds = new List<string>()
            };
            var user3 = new User()
            {
                Id = Guid.NewGuid().ToString(),
                Name = "user 3",
                RoleIds = new List<string>()
                {
                    "r1",
                    "r2"
                }
            };

            await context.Users.AddAsync(user1);
            await context.Users.AddAsync(user2);
            await context.Users.AddAsync(user3);
        }
    }

    public class Context : CouchContext
    {
        public CouchDatabase<User> Users { get; set; }

        protected override void OnConfiguring(CouchOptionsBuilder optionsBuilder)
        {
            optionsBuilder
                .UseEndpoint("http://192.168.56.56:5984")
                .EnsureDatabaseExists()
                .UseBasicAuthentication(username: "developer", password: "***")
                .ConfigureFlurlClient(settings =>
                {
                    settings.AfterCall = OnAfterCall;
                });
        }

        private void OnAfterCall(FlurlCall obj)
        {
            Console.WriteLine($"--- {obj.Request.Verb} {obj.Request.Url}\n{obj.RequestBody}");
        }

        protected override void OnDatabaseCreating(CouchDatabaseBuilder databaseBuilder)
        {
            databaseBuilder.Document<User>()
                .ToDatabase($"count_issue_users");
        }
    }

    public class User : CouchDocument
    {
        public string Name { get; set; }
        public List<string> RoleIds { get; set; }
    }
}

The output is similar to:

--- PUT http://192.168.56.56:5984/count_issue_users

--- PUT http://192.168.56.56:5984/count_issue_users/2e871230-8438-4bcc-8a1a-0d465896db5d
{"_conflicts":[],"name":"user 1","roleIds":["r1"],"_id":"2e871230-8438-4bcc-8a1a-0d465896db5d"}
--- PUT http://192.168.56.56:5984/count_issue_users/9069a548-b550-410a-b1c8-2acd5d420616
{"_conflicts":[],"name":"user 2","roleIds":[],"_id":"9069a548-b550-410a-b1c8-2acd5d420616"}
--- PUT http://192.168.56.56:5984/count_issue_users/16cba9e3-c5dd-4a29-838d-9d3996255880
{"_conflicts":[],"name":"user 3","roleIds":["r1","r2"],"_id":"16cba9e3-c5dd-4a29-838d-9d3996255880"}
--- POST http://192.168.56.56:5984/count_issue_users/_find
{"selector":{"roleIds.count":{"$gt":0}},"limit":2147483647}


usersWithRoles: 0

The expected result of usersWithRoles would be a list with the two users "user 1" and "user 3".

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdone

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions