Vb.net条件最后一位数检查

时间:2016-02-08 16:05:12

标签: vb.net linq

RuGroup将包含这样的内容......

MA1,MA2,MA3,MA4,MA5,MA6,MA7,MA8,MA9

我检查了MA和长度。如何检查第3位数字是否为数字?因为如果值存储为MAS,我需要显示它。请让我知道如何使用EndsWith属性或其他东西..

getRetailUgrp = (From retailUnitGrp In RetailUnits Where Not (retailUnitGrp.RuGroupName.ToString.StartsWith("MA") And retailUnitGrp.RuGroupName.ToString.Length = 3)).ToList()

4 个答案:

答案 0 :(得分:2)

您可以使用Char.IsDigit

Dim invalids = From rug in RetailUnits 
               Where rug.RuGroupName.Length < 3 _
                 OrElse Not rug.RuGroupName.StartsWith("MA") _
                 OrElse Not Char.IsDigit(rug.RuGroupName(2))

答案 1 :(得分:1)

好的,好好尝试这样的......这是winform代码。创建一个新的winforms应用程序并在表单上放置2个ComboBox控件,然后复制\粘贴下面的代码......

Imports System.Text.RegularExpressions

Public Class Form1

Dim RetailUnits As New List(Of RetailUnit)

Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
    Dim getRetailUgrp1 As New List(Of RetailUnit)
    Dim regex1 As Regex = New Regex("")
    If DirectCast(sender, System.Windows.Forms.ComboBox).SelectedItem < 3 Then
        regex1 = New Regex("^([A-Z]{2,3})+$")
    Else
        regex1 = New Regex("^([A-Z]{2}[0-9]{1})+$")
    End If
    Try
        ComboBox2.DataSource = (From retailUnitGrp In RetailUnits Where (retailUnitGrp.RuGroupName.ToString = regex1.Match(retailUnitGrp.RuGroupName.ToString).Value)).ToList()
        ComboBox2.DisplayMember = "RuGroupName"
    Catch ex As Exception

    End Try
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA1"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA2"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA3"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA4"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA5"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA6"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA7"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA8"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA9"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "AU"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "AUW"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "AS"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MA"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MAR"})
    RetailUnits.Add(New RetailUnit With {.RuGroupName = "MAS"})

    Dim ComboBox1Items() As String = New String() {"1", "2", "3"}
    ComboBox1.Items.AddRange(ComboBox1Items)
End Sub
End Class


Class RetailUnit
Private _RuGroupName As String
Public Property RuGroupName() As String
    Get
        Return _RuGroupName
    End Get
    Set(ByVal value As String)
        _RuGroupName = value
    End Set
End Property

End Class

答案 2 :(得分:0)

如果您确定每个组件的长度至少为三个字符,则可以在C#中执行以下操作(应直接转换为VB.Net)

Char.IsDigit(retailUnitGrp.RuGroupName[2])

这将返回第三位数字(从0开始的索引)是一个数字。

答案 3 :(得分:-1)

最后我用了这个。

If CBool(IIf(cboCountrySpec.SelectedValue.Equals(PIAConstants.CountrySpecOne), True, False)) OrElse _
                CBool(IIf(cboCountrySpec.SelectedValue.Equals(PIAConstants.CountrySpecTwo), True, False)) Then
                'getRetailUgrp = (From retailUnitGrp In RetailUnits Where Not retailUnitGrp.RuGroupName.ToString.Contains("MA")).ToList()                
                getRetailUgrp = (From retailUnitGrp In RetailUnits Where Not (retailUnitGrp.RuGroupName.ToString.StartsWith("MA") And retailUnitGrp.RuGroupName.ToString.Length = 3 And ((retailUnitGrp.RuGroupName.EndsWith("1")) Or (retailUnitGrp.RuGroupName.EndsWith("2")) Or
                (retailUnitGrp.RuGroupName.EndsWith("3")) Or (retailUnitGrp.RuGroupName.EndsWith("4")) Or (retailUnitGrp.RuGroupName.EndsWith("5")) Or (retailUnitGrp.RuGroupName.EndsWith("6")) Or (retailUnitGrp.RuGroupName.EndsWith("7")) Or (retailUnitGrp.RuGroupName.EndsWith("8")) Or
                (retailUnitGrp.RuGroupName.EndsWith("9"))))) 'IKEA00879697
            Else
                'getRetailUgrp = (From retailUnitGrp In RetailUnits Where retailUnitGrp.RuGroupName.ToString.Contains("MA")).ToList()                
                getRetailUgrp = (From retailUnitGrp In RetailUnits Where (retailUnitGrp.RuGroupName.ToString.StartsWith("MA") And retailUnitGrp.RuGroupName.ToString.Length = 3 And ((retailUnitGrp.RuGroupName.EndsWith("1")) Or (retailUnitGrp.RuGroupName.EndsWith("2")) Or
                (retailUnitGrp.RuGroupName.EndsWith("3")) Or (retailUnitGrp.RuGroupName.EndsWith("4")) Or (retailUnitGrp.RuGroupName.EndsWith("5")) Or (retailUnitGrp.RuGroupName.EndsWith("6")) Or (retailUnitGrp.RuGroupName.EndsWith("7")) Or (retailUnitGrp.RuGroupName.EndsWith("8")) Or
                (retailUnitGrp.RuGroupName.EndsWith("9"))))) 'IKEA00879697
            End If