Linq:方法无法转换为存储表达式
Linq: method cannot be translated into a store expression
我目前正在尝试限制哪些用户可以访问哪些组,并且正在使用 linq 来执行此操作。当我将相关组添加到视图时出现问题。
我不断收到的错误是:
LINQ to Entities does not recognize the method 'System.String
GetUserId(System.Security.Principal.IIdentity)' method, and this
method cannot be translated into a store expression
这是我的代码:
var groupUser = db.GroupUsers.Where(u => u.ApplicationUserId == User.Identity.GetUserId()).Select(gr => gr.GroupId);
pv.GroupList = db.Groups.Where(g => groupUser.Contains(g.Id)).ToList();
return View(pv);
您需要在 lambda 表达式之外调用 GetUserId()
,因为 Linq to Entities 无法将其转换为相应的 SQL(当然没有 SQL 替代 GetUserId()
):
var userId = User.Identity.GetUserId();
var groupUser = db.GroupUsers.Where(u => u.ApplicationUserId == userId)
.Select(gr => gr.GroupId);
我目前正在尝试限制哪些用户可以访问哪些组,并且正在使用 linq 来执行此操作。当我将相关组添加到视图时出现问题。
我不断收到的错误是:
LINQ to Entities does not recognize the method 'System.String GetUserId(System.Security.Principal.IIdentity)' method, and this method cannot be translated into a store expression
这是我的代码:
var groupUser = db.GroupUsers.Where(u => u.ApplicationUserId == User.Identity.GetUserId()).Select(gr => gr.GroupId);
pv.GroupList = db.Groups.Where(g => groupUser.Contains(g.Id)).ToList();
return View(pv);
您需要在 lambda 表达式之外调用 GetUserId()
,因为 Linq to Entities 无法将其转换为相应的 SQL(当然没有 SQL 替代 GetUserId()
):
var userId = User.Identity.GetUserId();
var groupUser = db.GroupUsers.Where(u => u.ApplicationUserId == userId)
.Select(gr => gr.GroupId);