我是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
}
如果有人可以帮助我解决这个问题,我会真的很感激。
谢谢,
卡皮尔
答案 0 :(得分: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();
}
}
答案 1 :(得分:0)
尝试:
var nonContractEmployees = session.Query<User>()
.Where(x => !x.Roles.Any(y => y.Type == UserType.Contract))
.ToList();
我已经更新了我的查询应该是什么。 (目前失败)
我已经向Ayende提交了一个失败的测试,以便您对Google Group的查询提出质疑。
https://groups.google.com/forum/?fromgroups#!topic/ravendb/LpSo3VdkavI