【问题标题】:checking value in datatable检查数据表中的值
【发布时间】:2017-07-25 02:26:36
【问题描述】:

我有这个包含 cell_1 和 cell_2 列的数据表值。

我想检查数据表中是否已经存在该值。我试过使用dt.contain("textbox1.text"),但这是错误的,因为在这个数据表中没有主键。我也试过像string x = dt_aa.select("cell_1 = textbox1.text")这样使用数据过滤器。

效果很好,但是当我尝试输入 texbox1.text 时,cell_1 列中没有值。它给了我一个错误,因为它不存在。我的最终方法是使用这个:

For Each dw As DataRow In dt_aa.Rows
    If dw("cell_1").ToString() = textbox1.text Then
        XtraMessageBox.Show("Cell_1 already exist !", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Exit For
    End If

    If dw("cell_2").ToString() = textbox2.text Then
        XtraMessageBox.Show("Cell_2 already exist !", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Exit For
    End If
Next

有没有办法简化它?因为我需要检查至少 4 个具有特定值的列。我担心循环需要一段时间来处理(CMIIW)。我尝试使用 LINQ,但我不太明白。

【问题讨论】:

    标签: vb.net linq datatable


    【解决方案1】:

    如果你想检查一个数据集合是否具有一定的价值,你可以在Linq中使用Any()方法。

    Linq 方法语法(带 lambda 表达式)

    Dim foundCell_1 = dt_aa.AsEnumerable.Any(Function (x) x.Field(Of String)("cell_1") = textBox1.Text)
    If foundCell_1 Then
        XtraMessageBox.Show("Cell_1 already exist !", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    End If
    
    If Not foundCell_1 Then
        Dim foundCell_2 = dt_aa.AsEnumerable.Any(Function(x) x.Field(Of String)("cell_2") = textBox2.Text)
        If foundCell_2 Then
            XtraMessageBox.Show("Cell_2 already exist !", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
    End If
    

    Linq 查询语法

    Dim foundCell_1 = (From rows In dt_aa.AsEnumerable
                      Where rows.Field(Of String)("cell_1") = textBox1.Text).Any
    If foundCell_1 Then
        XtraMessageBox.Show("Cell_1 already exist !", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    End If
    
    If Not foundCell_1 Then
        Dim foundCell_2 = (From rows In dt_aa.AsEnumerable
                          Where rows.Field(Of String)("cell_2") = textBox2.Text).Any
        If foundCell_2 Then
            XtraMessageBox.Show("Cell_2 already exist !", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
    End If
    

    如果不想使用Linq,可以使用DataTable中的Select()方法。

    Dim foundCell_1 = dt_aa.Select("cell_1 = '" & textBox1.Text & "'").Length > 0
    If foundCell_1 Then
        XtraMessageBox.Show("Cell_1 already exist !", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    End If
    
    If Not foundCell_1 Then
        Dim foundCell_2 = dt_aa.Select("cell_2 = '" & textBox2.Text & "'").Length > 0
        If foundCell_2 Then
            XtraMessageBox.Show("Cell_2 already exist !", "", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
    End If
    

    Linq Any() 方法可能执行得更快,因为如果元素匹配条件,它会返回 true,因此它不需要检查所有数据。

    注意:我假设 cell_1 和 cell_2 都是字符串。

    【讨论】:

    • 如果你介意,那是 linq 吗?我在哪里可以学习它?它对我来说仍然是陌生的语言:D
    • 这是 Linq。 Linq 有两种方法,首先是使用方法语法(就像我所做的那样)。其次是使用查询语法(如 SQL 查询)。
    • 如果你想在 VB.net 中学习 Linq,请查看 link
    猜你喜欢
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    相关资源
    最近更新 更多