简体   繁体   English

LINQ to Entities 无法识别“System.Object get_Item(System.String)”方法

[英]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.

相关问题 LINQ to Entities无法识别方法&#39;System.Object get_Item(System.String)&#39; - LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)' LINQ to Entities 无法识别方法“System.Object get_Item(System.String)”方法无法转换为存储表达式 - LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)' method cannot be translated into a store expression LINQ to Entities无法识别方法&#39;System.String get_Item(System.String)&#39;, - LINQ to Entities does not recognize the method 'System.String get_Item (System.String)', LINQ to Entities 无法识别“System.String get_Item(Int32)”方法 - LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method Linq to entities无法识别system.string get_Item方法 - Linq to entities does not recognize the method system.string get_Item LINQ to Entities无法识别方法&#39;System.String get_Item(Int32)&#39;,并且该方法无法转换为商店表达式 - LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression LINQ to Entities无法识别方法&#39;Newtonsoft.Json.Linq.JToken get_Item(System.String)&#39;方法, - LINQ to Entities does not recognize the method 'Newtonsoft.Json.Linq.JToken get_Item(System.String)' method, LINQ to Entities无法识别方法&#39;System.Object Parse(System.Type,System.String)&#39; - LINQ to Entities does not recognize the method 'System.Object Parse(System.Type, System.String)' LINQ to Entities无法识别方法&#39;System.String Concat(System.Object)&#39;方法, - LINQ to Entities does not recognize the method 'System.String Concat(System.Object)' method, LINQ 实体无法识别方法 '<>f__AnonymousType4`1[System.String] get_Item(Int32)' - LINQ to Entities does not recognize the method '<>f__AnonymousType4`1[System.String] get_Item(Int32)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM