【问题标题】:LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)', store expressionsLINQ to Entities 无法识别方法“System.Object get_Item(System.String)”,存储表达式
【发布时间】:2020-02-19 13:46:05
【问题描述】:

我正在为我的学期末项目开发一个房地产经纪人门户网站,我想生成我的列表,其中listedByID 等于用户登录时创建的会话。 这是代码

public ActionResult Index()
        {
            if ((string)Session["Role"] != "Private Seller")
            {
                return RedirectToAction("Login", "Account");
            }
            var data = db.tbl_listings.Where(x => x.l_listedByID == (int)Session["uID"]);
            return View(data.ToList());
        }

问题是它抛出了这个异常:

LINQ to Entities 无法识别方法“System.Object” get_Item(System.String)' 方法,这个方法不能翻译 到商店表达式中。

我也试过了:

public ActionResult Index()
        {
            if ((string)Session["Role"] != "Private Seller")
            {
                return RedirectToAction("Login", "Account");
            }
            var data = db.tbl_listings.Where(x => x.l_listedByID == (int)Session["uID"]).ToList();
            return View(data);
        }

任何帮助将不胜感激,我迷路了,问题似乎只出在控制器上。当代码点击“ToList()”方法时,问题就出现了。

【问题讨论】:

  • l_listedByID 的类型是什么?

标签: asp.net-mvc linq model-view-controller


【解决方案1】:

尝试在 linq 查询之外存储 Session["uID"] 的结果。

var uID = (int)Session["uID"];
var data = db.tbl_listings.Where(x => x.l_listedByID == uID).ToList();

这里的问题很可能出在 Session["uID"] 中,因为它的索引器和它被表达式作为方法而不是它返回的值捕获。 然后,它不能被翻译成实际的 SQL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 2013-04-07
    • 2012-04-22
    • 2017-01-02
    • 1970-01-01
    相关资源
    最近更新 更多