3

我是 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
    }

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

谢谢,
卡皮尔

4

2 回答 2

0

尝试:

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

于 2012-06-11T06:56:53.927 回答
0

编辑 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();
    }
}
于 2012-06-11T06:40:39.820 回答