gpt4 book ai didi

entity-framework - 返回 EF 模型类

转载 作者:行者123 更新时间:2023-12-04 06:25:01 25 4
gpt4 key购买 nike

我可以像这样从我的 Controller 成功返回一个模型:

return View(lemonadedb.Messages.ToList() );

我的观点完美地解释了它。

现在我只想显示 Messages.user == Membership.GetUser().ToString() 的消息。

但是当我这样做时:
return View(lemonadedb.Messages.Where( p => p.user == Membership.GetUser().ToString()).ToList());

我得到:

'LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.'



我需要一些方法来缩小消息表的结果。

我应该以某种方式使用 find() 方法吗?我还以为只有身份证

我该怎么做?

最佳答案

您遇到此问题的原因是 Entity Framework 正在尝试评估表达式 Membership.GetUser().ToString()进入 SQL 查询。您需要创建一个新变量来存储此表达式的值并将其传递到您的查询中。然后 Entity Framework 将按照您的预期解释它。

以下应该工作:

var user = Membership.GetUser().ToString();
return View(lemonadedb.Messages.Where(p => p.user == user).ToList());

我怀疑这是人们在编写 Entity Framework 查询时犯的一个非常常见的错误。

关于entity-framework - 返回 EF 模型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6149315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com