gpt4 book ai didi

c# - 如何在 RavenDB 中的对象集合中搜索 "Not Contains"条件

转载 作者:行者123 更新时间:2023-11-30 18:38:45 24 4
gpt4 key购买 nike

我是 RavenDB 的新手,我对这里的情况感到震惊。基本上这就是我想要实现的目标我有一个 RavenDB 中的员工列表,我需要获取不属于“类型”的员工契约(Contract)”。我尝试使用基本的 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
}

如果有人能帮助我解决这个问题,我将不胜感激。

谢谢,
卡 PIL

最佳答案

编辑 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();
}
}

关于c# - 如何在 RavenDB 中的对象集合中搜索 "Not Contains"条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10946144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com