我正在尝试根据用户角色过滤我的活动。只允许经理查看包含角色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
我收到了错误,显然我做错了。这是正确的方法还是有更好的方法?
答案 0 :(得分:2)
这需要RavenDB在查询期间进行计算,试试这个,而不是:
.Where(x => x.Roles.In(user.Roles))
(可能需要为In
扩展方法Raven.Client.Linq
,IIRC添加using语句。