在返回多个项目的 LINQ 表达式中获取成员资格数据时出错

发布于 2025-01-04 06:57:46 字数 859 浏览 3 评论 0原文

我正在尝试创建一个显示员工列表的页面。

所有员工身份验证数据都存储在 .NET 成员资格表中,名称等其他数据存储在名为“Employees”的相关表中。

这是我获取员工列表的代码:

var viewModel = employees.OrderBy(e => e.Name).
                            Select(e => new EmployeeViewModel
                            {
                                EmployeeId = e.EmployeeID,
                                Name = e.Name,
                                Email = Membership.GetUser(e.UserID).Email,
                                Active = e.Active
                            });

我收到此错误:

LINQ to Entities 无法识别“System.Web.Security.MembershipUser GetUser(System.Object)”方法,并且此方法无法转换为存储表达式。

我做了一些研究,发现我需要将 Membership.GetUser 函数拉到 LINQ 表达式之外。如果我只得到一名员工,那么这会非常有用,但我返回一个列表,因此每次迭代的用户都会不同。

有人知道我该如何解决这个问题吗?

I'm trying to create a page that displays a list of employees.

All the employee authentication data is stored in the .NET membership tables, with other data like Name stored in a related table named Employees.

Here's my code that is getting the list of employees:

var viewModel = employees.OrderBy(e => e.Name).
                            Select(e => new EmployeeViewModel
                            {
                                EmployeeId = e.EmployeeID,
                                Name = e.Name,
                                Email = Membership.GetUser(e.UserID).Email,
                                Active = e.Active
                            });

I get this error:

LINQ to Entities does not recognize the method 'System.Web.Security.MembershipUser GetUser(System.Object)' method, and this method cannot be translated into a store expression.

I did some research, and found that I need to pull the Membership.GetUser function outside of the LINQ expression. This would work great if I was only getting a single employee, but I'm returning a list, so the user would be different on each iteration.

Anyone know how I can fix this?

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

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

发布评论

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

评论(1

秋日私语 2025-01-11 06:57:46

您可以通过在 Select(..) 之前插入 ToArray() 来在“客户端”端评估您的选择:

var viewModel = employees.OrderBy(e => e.Name)
                           .ToArray()
                           .Select(e => new EmployeeViewModel
                            {
                                EmployeeId = e.EmployeeID,
                                Name = e.Name,
                                Email = Membership.GetUser(e.UserID).Email,
                                Active = e.Active
                            });

在这种情况下,EF 不要尝试翻译 <代码>Membership.GetUser 方法。

编辑:
正如评论中指出的,该解决方案虽然解决了您的问题,但引入了另一个问题:它将执行 N+1 选择,其中 N 是员工数量。

为了避免这种情况,您可以在 EF 模型中映射 MemberShip 表,并在 MemberShipEmployee 表之间创建关系。然后,您可以一键获取所有数据,而无需使用 Membership.GetUser()

You can evaluate your select on the "client" side with inserting a ToArray() before the Select(..):

var viewModel = employees.OrderBy(e => e.Name)
                           .ToArray()
                           .Select(e => new EmployeeViewModel
                            {
                                EmployeeId = e.EmployeeID,
                                Name = e.Name,
                                Email = Membership.GetUser(e.UserID).Email,
                                Active = e.Active
                            });

In this case EF don't try to translate Membership.GetUser method.

EDIT:
As pointed out in the comments, this solution although solves your problem, but introduces another one: it will execute N+1 selects where N is the count of employees.

To avoid that you can map your MemberShip tables in your EF model and you can create an relation between the MemberShip and your Employee table. Then you can get all the data with one select and without using Membership.GetUser().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文