MVC Linq 选择具有以下角色的用户列表

发布于 2024-10-06 14:18:22 字数 536 浏览 0 评论 0原文

我下面的查询有什么问题?这是使用标准成员资格表。

var clientRole = rushDB.aspnet_Roles.Single(r => r.LoweredRoleName == "client" );
        //var users = rushDB.aspnet_Users.Where(u => u.aspnet_Roles.Contains(client)).AsEnumerable();

        var users = from u in rushDB.aspnet_Users
                        where u.aspnet_Roles.Contains(clientRole)
                    select u;

        return View(users.ToList());

我的观点出现了这个错误... 无法创建“RushToIt.Models.aspnet_Roles”类型的常量值。此上下文中仅支持原始类型(“例如 Int32、String 和 Guid”)。

What is wrong with my query below? This is using the standard Membership tables.

var clientRole = rushDB.aspnet_Roles.Single(r => r.LoweredRoleName == "client" );
        //var users = rushDB.aspnet_Users.Where(u => u.aspnet_Roles.Contains(client)).AsEnumerable();

        var users = from u in rushDB.aspnet_Users
                        where u.aspnet_Roles.Contains(clientRole)
                    select u;

        return View(users.ToList());

I get this error on my view...
Unable to create a constant value of type 'RushToIt.Models.aspnet_Roles'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红墙和绿瓦 2024-10-13 14:18:22

我希望您正在尝试(或者更确切地说 LINQ 正在尝试)使用 clientRole 作为 select 语句的 where 子句。您只能发送简单类型的参数。

相反,您需要内联评估角色。

但是,如果您只是反向遍历,可能会更容易(听起来不错,对吧?)。您应该能够执行以下操作:

var clientRole = rushDB.aspnet_Roles.Single(r => r.LoweredRoleName == "client" );
return View(clientRole.aspnet_Users.ToList());

I would expect that you are trying (or rather LINQ is trying) to use the clientRole as a where-clause to a select statement. You can only send simple types are parameters.

Instead you would need to evaluate the role inline.

However it's probably easier if you just traverse in reverse (that sounds good, right?). You should be able to do this:

var clientRole = rushDB.aspnet_Roles.Single(r => r.LoweredRoleName == "client" );
return View(clientRole.aspnet_Users.ToList());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文