RavenDb 4:检查字符串数组的字符串是否存在于不同的字符串数组中

RavenDb 4: Check if a string of an array of strings exists in different array of strings

我正在尝试根据用户角色过滤我的活动。经理只能查看包含角色 Manager 的活动。有关详细信息,请参阅下文。

数据:

var user = new User
{
    Roles = [
        "Manager"
    ]   
}

var activities = new List<Activity>
{
    new Activity
    {
        Description = "My First Activity",
        Roles = [
            "Admin",
            "Manager"
        ]
    },  
    new Activity
    {
        Description = "My Second Activity",
        Roles = [
            "Admin"
        ]
    },  
    new Activity
    {
        Description = "My Third Activity",
        Roles = [
            "Manager",
            "Client"
        ]
    },
}

过滤 activity

的查询
 var filtered = await context.Query<Activity>()
                    .Where(x => x.Roles.Any(y => user.Roles.Contains(y)))
                    .ToListAsync();

过滤后的预期结果:

[
    {
        new Activity
        {
            Description = "My First Activity",
            Roles = [
                "Admin",
                "Manager"
            ]
        }
        new Activity
        {
            Description = "My Third Activity",
            Roles = [
                "Manager",
                "Client"
            ]
        },
    }
]

查询错误

System.InvalidOperationException : Can't extract value from expression of type: Parameter

我收到一个错误,很明显我做错了什么。这是正确的方法还是有更好的方法?

这需要 RavenDB 在查询期间进行计算,试试这个,而不是:

.Where(x => x.Roles.In(user.Roles))(可能需要为 In 扩展方法添加 using 语句,Raven.Client.Linq,IIRC)。