0

I have a small question about my project you could not know, I understand. But perhaps I have a very simple fault in my following function you could see?

<OperationContract()>
Public Function GetBestFahrer() As Autofahrer
    Dim Database As New Model1Container
    Dim Fahrer As Autofahrer = From d In Database.AutofahrerSet Where d.Unfälle = 0 Select d Order By d.Unfälle Ascending
    If Fahrer Is Nothing Then
        Fahrer = Autofahrer.CreateAutofahrer(0, "Testfahrer", DateTime.Now, 0)
        Database.AutofahrerSet.AddObject(Fahrer)
        Database.SaveChanges()
    End If

    Return Fahrer

End Function

Error message on line with "Dim Fahrer As Autofahrer = ..."

Unable to cast object of type 'System.Data.Objects.ObjectQuery`1[SilverlightApplication4.Web.Autofahrer]' to type 'SilverlightApplication4.Web.Autofahrer'.

1
  • 1
    You haven't accepted any answers to your questions. Commented May 3, 2011 at 19:26

1 Answer 1

1

Fahrer is a single object
From d In Database.AutofahrerSet Where d.Unfälle = 0 Select d Order By d.Unfälle Ascending does not give you a single object

It looks like you want to see if something exists and then create it if it doesn't.

Try something like this

Dim myCount as integer = (From d In Database.AutofahrerSet Where d.Unfälle = 0 Select d).Count()
if myCount = 0 then
    Fahrer = Autofahrer.CreateAutofahrer(0, "Testfahrer", DateTime.Now, 0)
    Database.AutofahrerSet.AddObject(Fahrer)
    Database.SaveChanges()
else
    Fahrer = (From d In Database.AutofahrerSet Where d.Unfälle = 0 Select d Order By d.Unfälle Ascending).First()
End If
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.