Search code examples
c#asp.netlinq-to-entities

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.

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. Can someone please help me sort this out? Thanks!


Solution

  • It means that it can't translate Session["UserId"] into SQL (it's not smart enough to pull the value out of the collection). I'd recommend storing this in a value before using it:

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