0

I am working on MVC4 App, and I am stuck at one point, I tried using google to help me, but without success. This might be more simple then I think, but coming from web forms and shifting to mvc is "painful" sometime. I am trying to loop through the model I have and get the values stored in that model. I tried few approaches but I am getting an error everytime. This is what I have:

var modelAgentFilter = from s in _aa.Agents
                       where s.COUNTER == Convert.ToInt32(AgentID)
                       select s;
if (modelAgentFilter != null)
{                                         
    ViewBag.FirstName = // Get FirstName object here
}

Thanks in advance for your comments. Laziale

EDIT:

I did include for loop like this:

   if (modelAgentFilter != null)
                {
                    foreach (var property in modelAgentFilter)
                    {
                        string test = property.ADDRESS;
                    }           
                }

But when the compiler will reach the foreach step I am getting this error: "LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression."

I can get to the properties of the var model using that foreach look but as soon as the compiler will try to loop the model that error pops up. Thanks again

tereško
  • 58,060
  • 25
  • 98
  • 150
Laziale
  • 7,965
  • 46
  • 146
  • 262
  • 1
    What is the error? To be fair, looking at your code, it's not an MVC problem really. It's just a LINQ problem. – Jason Evans Sep 30 '13 at 15:34
  • I can see that the model is not null, but now how I can take a value from the model? for example modelAgentFilter.FirstName? Thanks – Laziale Sep 30 '13 at 15:36
  • http://stackoverflow.com/questions/11261590/mvc-razor-foreach – Matt Bodily Sep 30 '13 at 15:36
  • @MattBodily I know how to get the data in the razor view but I want to get the data before I am opening the view. I am sending another model in the view. Thanks – Laziale Sep 30 '13 at 15:38
  • Why don't you try converting AgentID outside of the Linq expression? – ediblecode Sep 30 '13 at 16:12

1 Answers1

0

LINQ to Entities does not recognize any methods. You can't use even ToString() in LINQ expression. You need first convert your value and than add it in LINQ.

In your example you need to do something like following:

var _agentID = int.Parse(AgentID);

var modelAgentFilter = from s in _aa.Agents
                       where s.COUNTER == _agentID 
                       select s;
Andrey Gubal
  • 3,481
  • 2
  • 18
  • 21