0

The problem:

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[System.Int32]', but this dictionary requires a model item of type 'migros.Models.State'.

What I'm trying to do

I need to pass the result of the following linq query to a View.

using (var db = new migros_mockEntities1())
        {
            var listOfIdeas = (from x in db.States select x.ID); 

            return View(listOfIdeas);
        }

The View requires IEnumerable, but it seems I can't cast the result of the linq query to IEnumerable. I'm using entity framework database first approach.

tereško
  • 58,060
  • 25
  • 98
  • 150
Marius Balaban
  • 197
  • 4
  • 12

1 Answers1

1

The problem is that you trying to return ObjectQuery from within the using block. Try to materialize your object-set

var listOfIdeas = (from x in db.States select x.ID).ToList(); 

Also, dont forget, that dealing with context can be tricky. In your case var listOfIdeas = (from x in db.States select x.ID) is just a query, that will run only when you'd begin to iterate over it. So, if context gets already disposed you'd get an exception, trying to use listOfIdeas.

Konstantin Chernov
  • 1,899
  • 2
  • 21
  • 37
  • I get an exception, one that says I'm trying to pass an object of type System.Collections.Generic.List and it requires one of type System.Collections.Generic.IEnumerable – Marius Balaban Aug 09 '12 at 09:09
  • Eureka! That was it, the view was expecting an object of a type defined by me and, dumb me, I was sending an int to it. Thx for the help. – Marius Balaban Aug 09 '12 at 09:18