[英]LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)' method
In trying to output data to a view I an get the following error.在尝试将数据输出到视图时,我收到以下错误。 LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)' method, and this method cannot be translated into a store expression.
LINQ to Entities 无法识别方法 'System.Object get_Item(System.String)' 方法,并且此方法无法转换为存储表达式。
Controller code:控制器代码:
public ActionResult Index()
{
return View(ReturnResults());
}
public IQueryable<ShippingRequest> ReturnResults()
{
RatesModel db = new RatesModel();
IQueryable<ShippingRequest> response = db.ShippingRequests;
var results = response
.Where(r => r.UserId == (int)Session["UserId"]);
return results;
}
View Code:查看代码:
@model IEnumerable<ShippingRatesApp.Models.ShippingRequest>
@{
ViewBag.Title = "Get Rates";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h3>Shipping Rates</h3>
@{
<table>
@foreach (var rate in Model)
{
<tr>
<td>@rate.ShipRate</td>
</tr>
}
</table>
}
I am very new to c# and none of the web searches I have come across have solved my issue.我对 c# 很陌生,我遇到的所有网络搜索都没有解决我的问题。 Can someone please help me sort this out?
有人可以帮我解决这个问题吗? Thanks!
谢谢!
It means that it can't translate Session["UserId"]
into SQL (it's not smart enough to pull the value out of the collection).这意味着它无法将
Session["UserId"]
转换为 SQL(它不够聪明,无法从集合中提取值)。 I'd recommend storing this in a value before using it:我建议在使用它之前将其存储在一个值中:
var currentUserId = (int)Session["UserId"];
var results = response
.Where(r => r.UserId == currentUserId);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.