【问题标题】:How to search for "Not Contains" condition on collection of objects in RavenDB如何在 RavenDB 中的对象集合上搜索“不包含”条件
【发布时间】:2012-06-08 09:21:20
【问题描述】:

我是 RavenDB 的新手,我对这里的一个条件感到震惊。基本上这就是我想要实现的目标我有一个 RavenDB 中的员工列表,我需要获取不属于“类型”的员工合同”。我尝试使用基本的 linq 查询,但我无法解决问题,因为我遇到了“不支持方法”和“无法理解如何翻译……等”等异常。

这是我到目前为止所尝试的。

   class Program
    {
        static void Main(string[] args)
        {
            using (var documentStore = new DocumentStore() { ConnectionStringName = "RavenDBConnectionString" })
            {
                documentStore.Initialize();
                /* using (var session = documentStore.OpenSession())
                {
                    session.Store(new User { Id = 1, Roles = new List<Role> { new Role { Type = UserType.Contract }, new Role { Type = UserType.Developer } } });
                    session.Store(new User { Id = 2, Roles = new List<Role> { new Role { Type = UserType.Permanent }, new Role { Type = UserType.Developer } } });
                    session.Store(new User { Id = 3, Roles = new List<Role> { new Role { Type = UserType.SeniorDeveloper }, new Role { Type = UserType.Manager } } });
                    session.Store(new User { Id = 4, Roles = new List<Role> { new Role { Type = UserType.Contract }, new Role { Type = UserType.SeniorDeveloper } } });
                    session.Store(new User { Id = 5, Roles = new List<Role> { new Role { Type = UserType.Permanent }, new Role { Type = UserType.Manager } } });
                    session.Store(new User { Id = 6, Roles = new List<Role> { new Role { Type = UserType.Contract }, new Role { Type = UserType.Developer } } });
                    session.SaveChanges();
                }*/

                using (var session = documentStore.OpenSession())
                {

                    //var nonContractEmployees = session.Query<User>().Where(x => !x.Roles.Exists(y => y.Type == UserType.Contract)).ToList();
                     var nonContractEmployees = session.Query<User>().Where(x => x.Roles.Count(y => y.Type == UserType.Contract) == 0).ToList();
                    // var nonContractEmployees = session.Query<User>().Where(x => !x.Roles.Contains(x.Roles.FirstOrDefault(y => y.Type == UserType.Contract))).ToList();
                    //var nonContractEmployees = session.Query<User>().Where(x => x.Roles.FirstOrDefault(y => y.Type == UserType.Contract) == null).ToList();
                }
            }
        }
    }

    public class User
    {
        public int Id { get; set; }
        public List<Role> Roles { get; set; }
    }

    public class Role
    {
        public UserType Type { get; set; }
    }

    public enum UserType
    {
        Manager,
        Permanent,
        Contract,
        Developer,
        SeniorDeveloper
    }

如果有人能帮助我解决这个问题,我真的很感激。

谢谢,
卡皮尔

【问题讨论】:

标签: c# linq ravendb


【解决方案1】:

编辑 1

var nonContractEmployees = session.Query<User>()
    .Where(x => !x.Roles.In(new [] {new Role { Type = UserType.Contract}))
    .ToList();

编辑 2

这可行,但效率不高。

using (var session = documentStore.OpenSession())
{
    var nonContractEmployees = session.Query<User>()
        .ToList()
        .Where(x => x.Roles.Contains(
            new Role {Type = UserType.Contract},
            new RoleComparer()));                        
}

public class RoleComparer : IEqualityComparer<Role>
{
    public bool Equals(Role x, Role y)
    {
        return
            x == null && y == null ||
            x != null &&
            y != null &&
            x.Type == y.Type;
    }

    public int GetHashCode(Role obj)
    {
        return
            obj == null
                ? 0
                : obj.Type.GetHashCode();
    }
}

【讨论】:

  • @M Afifi:不可能是朋友,“y”的类型是“Role”而不是 UserType
  • @M Afifi:感谢它按我的要求工作,但是我们有什么办法可以在服务器端本身做到这一点?如果我们要转换为不可能的列表,我想在查询中使用统计信息。我想我将不得不像 Ayende 在这里建议的那样选择索引 groups.google.com/forum/?fromgroups#!topic/ravendb/LpSo3VdkavI 。我将此标记为答案,因为这符合我的要求。
  • @wizzardz 我也在考虑索引,但他们真的很懒惰。它是一个需要修复的错误。他们的库在语法上是不正确的。
【解决方案2】:

试试:

var nonContractEmployees = session.Query<User>()
    .Where(x => !x.Roles.Any(y => y.Type == UserType.Contract))
    .ToList();

我已将我的查询更新为应有的内容。 (目前失败)

我已经向 Ayende 提交了一个失败的测试,以解决您在 Google 网上论坛上的查询。

https://groups.google.com/forum/?fromgroups#!topic/ravendb/LpSo3VdkavI

【讨论】:

  • @MAfifi 逻辑是 'x.Roles.Any(..)' 这将返回 true,然后用 !使其为假。任何具有该角色的用户 (true) 不包括他们。这对我来说是一个错误。
  • 我在 1.0.960 中也看到了这种行为
猜你喜欢
  • 2013-06-07
  • 2017-03-23
  • 2011-07-23
  • 1970-01-01
  • 2013-01-27
  • 1970-01-01
  • 1970-01-01
  • 2018-01-05
  • 2020-06-15
相关资源
最近更新 更多