我是 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
}
如果有人可以帮助我解决这个问题,我真的很感激。
谢谢,
卡皮尔