0
votes

I am developing a realtor's portal for my semester end project and I wanted to generate a list of my listings where the listedByID is equal to a session which is created when the user is logged in. here is the code

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

The problem is it is throwing this exception:

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.

I have tried this as well:

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

Any help would be appreciated, I am lost and the problem seems to be in the controller only. The problem arises when the code hits the "ToList()" method.

1
What is the type of l_listedByID? - Lajos Arpad

1 Answers

1
votes

Try store result of Session["uID"] outside of linq query.

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

There is a big chance that the problem here is in Session["uID"], since its indexer and its being captured by expression as a method and not by the value it returns. Then, it cannot be translated to an actual SQL.