Linq:无法将方法转换为商店表达式 [英] Linq: method cannot be translated into a store expression

查看:68
本文介绍了Linq:无法将方法转换为商店表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试限制哪些用户可以访问哪些组,并通过使用linq来做到这一点.将相关组添加到视图时,出现了我的问题.

I am currently trying to limit which users can access which groups, and am doing this by using linq. My problem occours when I am adding the relevant groups to the view.

我一直遇到的错误是:

LINQ to Entities无法识别方法'System.String GetUserId(System.Security.Principal.IIdentity)'方法,以及此方法 方法无法转换为商店表达式

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()):

You need to call GetUserId() outside the lambda expression, as Linq to Entities is not able to translate it to corresponding SQL (of course there is no SQL alternative for GetUserId()):

var userId = User.Identity.GetUserId();
var groupUser = db.GroupUsers.Where(u => u.ApplicationUserId == userId)
                             .Select(gr => gr.GroupId);

这篇关于Linq:无法将方法转换为商店表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆