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

问题描述 投票:1回答:1

我试图根据用户角色过滤我的活动。只允许经理查看包含角色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"
        ]
    },
}

过滤活动的查询

 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

我收到了错误,显然我做错了。这是正确的方法还是有更好的方法?

linq ravendb ravendb4
1个回答
2
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.