我在我的 .aspx 页面中填充了一个 DataGrid,并在页面上有一些下拉菜单用于过滤出现在网格中的数据。我正在尝试使用 LINQ 过滤我在 page_load 和下拉菜单的 SelecedIndexChanged 事件上绑定的 DataTable 中的数据。
这是我目前的方法:
Dim filteredData As DataTable = (From d In rawDataTable
Select d).CopyToDataTable
If Me.cbFilter1.SelectedIndex > 0 Then
filteredData = (From f In filteredData
Where f.Field(Of Date)("ADateField").Year = Me.cbFilter1.SelectedValue
Select f).CopyToDataTable
End If
If Me.cbFilter2.SelectedIndex > 0 Then
filteredData = (From f In filteredData
Where f.Field(Of String)("AStringField") = Me.cbFilter2.SelectedValue
Select f).CopyToDataTable
End If
If Me.cbFilter3.SelectedIndex > 0 Then
filteredData = (From f In filteredData
Where f.Field(Of Boolean)("ABooleanField") = (cbFilter3.SelectedValue = "Yes")
Select f).CopyToDataTable
End If
' ...and finally binding my grid to filteredData
有没有更清洁和更有效的方法来做到这一点?你会怎么做?谢谢!