views:

82

answers:

4
     Function Edit(ByVal id As Integer) As ActionResult


        Dim entities As New Deployment_devEntities()
        Dim w As System.Data.Objects.ObjectQuery(Of tblWebsites) = entities.CreateQuery(Of tblWebsites)("SELECT VALUE m FROM tblWebsites as m WHERE m.WebsiteID = " & id)

        Dim q = From m In entities.tblWebsites Where m.WebsiteID = id Select m


        Return View(q.Single())

    End Function

I have a strong typed view of type EntityFrameworkTest.tblWebsites when I pass in the results using either Linq to Entity or Entity SQL I get this error

The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery`1[EntityFrameworkTest.tblWebsites]', but this dictionary requires a model item of type 'EntityFrameworkTest.tblWebsites'.

What am I doing wrong?

The code is example only.

A: 

Your action is returning a list, but your view expects 1 object. Figure out which one of these is wrong and fix it.

Craig Stuntz
his naming's all screwed up, but it looks like a "tblWebsites" is a single object.
Paul
He's passing a list. That's what ObjectQuery`1[[EntityFrameworkTest.tblWebsite] means. True, that doesn't correspond with his demo code, but he says the code isn't real, so I tend to believe the exception rather than his fake demo code.
Craig Stuntz
Fair enough, I was making the possibly erroneous assumption that his demo code was syntactically the same.
Paul
A: 

Either your view needs to inherit from IEnumerable<tblWebsites> or you need to change your controller to return a single instance of tblWebsites. You can do this with the LINQ .FirstOrDefault() method.

Robaticus
he's trying to do that already w/ the call to .Single()
Paul
Thanks, Paul. I missed the .Single().
Robaticus
The answer is not wrong, though. Despite the `Single()` in the demo code, the error indicates he's indeed returning a list in the real world.
Craig Stuntz
Thank you FirstOrDefault() worked fine.
Matthew Rygiel
+1  A: 

I don't normally do VB if I can avoid it, it looks to me like your code is inferring the type (and coming up with the assumption that it's an objectquery). Try calling .Single(Of tblWebsites) instead of just Single(). Or casting.

Paul
A: 

The problem turned out to be that the .Single() is not supported by LINQ to Entities. Using .FirstOrDefault() or .First() worked fine.

Thank you all.

Matthew Rygiel