SQL ADO.Net 实体 LINQ 问题

发布于 2024-11-04 12:01:16 字数 777 浏览 0 评论 0原文

我有一个关于我的项目的小问题你不可能知道,我理解。但也许我在下面的函数中有一个非常简单的错误,你可以看到吗?

<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

错误消息显示“Dim Fahrer As Autofahrer = ...”

无法转换类型的对象 'System.Data.Objects.ObjectQuery`1[SilverlightApplication4.Web.Autofahrer]' 键入 'SilverlightApplication4.Web.Autofahrer'。

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'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裂开嘴轻声笑有多痛 2024-11-11 12:01:16

Fahrer 是单个对象
From d In Database.AutofahrerSetWhere d.Unfälle = 0 Select d Order By d.Unfälle Ascending 不会给你一个单一的对象

看起来你想看看某个东西是否存在,然后创建它如果它存在没有。

尝试这样的事情

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文