【问题标题】:How to Find a DataTable Row by Column Value Using LINQ in VB.NET如何在 VB.NET 中使用 LINQ 按列值查找 DataTable 行
【发布时间】:2012-01-07 12:14:30
【问题描述】:

您好,我在运行时创建了一个动态数据表。设置是这样的

Id |  Name  | Age
-----------------
3  |  Mike  | 21
6  |  John  | 43
8  |  Sara  | 34

我想做的是想出一个 linq 语句,我可以用它来查找和更新特定的行。

将 AGE 更改为 '33' WHERE ID = '3'

的语句

我的代码到目前为止是:

-[VB.NET]-
Dim MyRow As DataRow = From column In MyTable.Rows Where column("Id") = 3
MyRow(0)("Age") = 33

但这不会更新我的 DataTable 条目。任何帮助,将不胜感激。谢谢。

【问题讨论】:

  • 嗨@Mike,考虑一个答案作为您的解决方案,以最好的方式解决您的问题并很好地描述它..

标签: vb.net linq datatable linq-to-objects datarow


【解决方案1】:

如果我错了,请纠正我。看看:

Dim row As DataRow = (From column In MyTable.Rows Where column("Id") = 3).FirstOrDefault()
If Not IsNothing(row) Then
row("Age") = 33
End If

或者你可以试试 DataTable.Select() 方法。

Dim rows=MyTable.Select("ID=3")

【讨论】:

【解决方案2】:

这不是LINQ,而是过滤数据表。

Dim iRow() As DataRow = ParticularSource.Tables(ParticularTable).Select(
    String.Format("ItemID = '{0}'", ParticularID))

ParticularSource 是我的数据集的名称
ParticularTable 是我的数据表的名称
ItemID 是我搜索的字段
ParticularID 是要搜索的值

iRow(0)("Age") = NewValue
ParticularSource.Tables(ParticularTable).AcceptChanges()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-26
    • 2015-10-22
    • 1970-01-01
    • 2011-09-28
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    相关资源
    最近更新 更多