【问题标题】:What is the VB.NET equivalent to this C# code?与此 C# 代码等效的 VB.NET 是什么?
【发布时间】:2010-03-09 18:47:26
【问题描述】:

VB.NET 等价于这个 C# 代码?

    ctx.Load(site,
                x => x.Lists.Where(l => l.Title != null));

我试过了

 ctx.Load(site, Function(x) x.Lists.Where(Function(l) l.Title IsNot Nothing))

"The expression (Convert(l.Title) != null) is not supported." 会出现此错误

想法

【问题讨论】:

  • Title 是一个恰好是 String 类型的属性

标签: vb.net c#-to-vb.net


【解决方案1】:

如果 Title 是字符串,请尝试使用 IsNullOrEmpty();

如果 Title 为 Nullable,则为 Nullable(Of T).HasValue

Sub Main()

        Dim list As New List(Of A)

        Dim a1 As New A
        a1.Title = "sqws"
        Dim a2 As New A
        a2.Title = Nothing


        list.Add(a1)
        list.Add(a2)

        Dim q = From c In list Where c.Title IsNot Nothing

    End Sub



    Public Class A

        Dim t As String

        Public Property Title() As String
            Get
                Title = t
            End Get
            Set(ByVal value As String)
                t = value
            End Set
        End Property

    End Class

【讨论】:

  • 我已经尝试过这些,并且 1) 类型不能为空,它是一个字符串。 2) 似乎无法在 lambda 语句中进行字符串比较...
【解决方案2】:

这可能是作弊,但我过去曾使用 Reflector 反编译我的 C# 代码,然后将其显示为其他 .NET 语言,只是为了看看我最熟悉 C# 时它们的外观

【讨论】:

    【解决方案3】:

    这真的应该工作:

    ctx.Load(site, Function(x) x.Lists.Where(Function(l) l.Title.IsNullOrEmpty = False))
    

    如果没有,请告诉我错误消息。

    【讨论】:

      【解决方案4】:

      你试过IsNothing函数吗?

      ctx.Load(site, Function(x) x.Lists.Where(Function(l) Not IsNothing(l.Title)))
      

      编辑:

      既然您已经指定 Title 是一个字符串,那么您应该使用 IsNullOrEmpty 函数。

      ctx.Load(site, Function(x) x.Lists.Where(Function(l) Not String.IsNullOrEmpty(l.Title)))
      

      【讨论】:

      • 当我在 Lambda 表达式中尝试此操作时,我收到一条错误消息,指出 IsNnothing 成员不能在表达式中使用...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      相关资源
      最近更新 更多