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

SGekko

在尝试将数据输出到视图时,出现以下错误。LINQ to Entities无法识别方法'System.Object get_Item(System.String)',并且该方法无法转换为商店表达式。

控制器代码:

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;
        }

查看代码:

@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>
}

我是C#的新手,遇到的所有网络搜索都无法解决我的问题。有人可以帮我解决这个问题吗?谢谢!

约翰

这意味着它不能转换Session["UserId"]为SQL(它不够聪明,无法从集合中提取值)。我建议在使用它之前将其存储为一个值:

var currentUserId = (int)Session["UserId"];
var results = response
            .Where(r => r.UserId == currentUserId);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

LINQ to Entities 无法识别方法“System.Object get_Item(System.String)”,存储表达式

LINQ to Entities无法识别方法'<> f__AnonymousType4`1 [System.String] get_Item(Int32)'

Linq to实体无法识别方法system.string get_Item

LINQ to Entities无法识别方法'System.String IfNullOrWhiteSpace'

LINQ to Entities无法识别方法ToDateTime(System.String)

LINQ to Entities无法识别方法'System.String get_Item(Int32)',并且该方法无法转换为商店表达式

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

LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)'方法

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

LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)'

LINQ to Entities无法识别方法'Int32 ToInt32(System.String)'方法

(MVC)LINQ to Entities无法识别方法“ Boolean VerifyHashedPassword(Byte [],System.String)”方法

LINQ to Entities无法识别方法'System.String ToString()'。尝试解析DateTime时发生错误

LINQ to Entities无法识别方法'System.String ToString(Int32)'

LINQ to Entities无法识别方法'Int32 IndexOf(System.String,System.StringComparison)'方法

LINQ to Entities无法识别方法'System.Collections.Generic.Dictionary`2 [System.Int32,System.String] ToDictionary

LINQ to Entities无法识别方法'System.String StringConvert(System.Nullable`1 [System.Double])

LINQ to Entities无法识别方法'System.String ToString()',并且该方法无法转换为商店表达式

ASP.NET实体框架LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)

将数据显示到下拉列表-“ LINQ to Entities无法识别方法'System.String ToString()'方法”

LINQ to Entities无法识别方法'Boolean Exists(System.Predicate`1 [Entities.Connection])'方法

LINQ to Entities不能识别方法'System.String [] ToArray [String],除非使用SqlQuery方法

LINQ to Entities无法识别方法'System.String sha256(System.String)',并且该方法无法转换为商店表达式

LINQ to Entities无法识别方法get_Item(Int32)'方法

LINQ to Entities无法识别方法'Single ToSingle(System.String)'方法,并且该方法无法转换为商店表达式

LINQ to Entities无法识别方法'Int32 Parse(System.String)'方法,并且该方法无法转换为商店表达式

LINQ to Entities无法识别方法'Double ToDouble(System.String)',并且该方法无法转换为商店表达式

LINQ to Entities无法识别方法String.Format

LINQ-到实体无法识别方法system.string ToString()异常