我试图弄清楚如何从Datatable获取我需要的数据。 我是数据表的新手,无法通过阅读文档来了解如何正确构建所需的查询。
我在PowerShell中制作了一个测试脚本,我发布在下面:
# clear the screen:
CLS
# Create the Datatable:
$dt1 = New-Object System.Data.Datatable
# Name the datatable:
$dt1.TableName = "MyTable"
# Add collumns to the table:
[void]$dt1.Columns.Add("datacolumnone")
[void]$dt1.Columns.Add("datacolumntwo")
[void]$dt1.Columns.Add("datacolumnthree")
# Add data:
[void]$dt1.Rows.Add([string]"id 1", [string]"fact 1", [string]"purple")
[void]$dt1.Rows.Add([string]"id 1", [string]"fact 2", [string]"black")
[void]$dt1.Rows.Add([string]"id 1", [string]"fact 3", [string]"green")
[void]$dt1.Rows.Add([string]"id 1", [string]"fact 4", [string]"green")
[void]$dt1.Rows.Add([string]"id 2", [string]"fact 1", [string]"blue")
[void]$dt1.Rows.Add([string]"id 2", [string]"fact 2", [string]"yellow")
[void]$dt1.Rows.Add([string]"id 2", [string]"fact 3", [string]"purple")
[void]$dt1.Rows.Add([string]"id 2", [string]"fact 4", [string]"yellow")
[void]$dt1.Rows.Add([string]"id 3", [string]"fact 1", [string]"purple")
[void]$dt1.Rows.Add([string]"id 3", [string]"fact 2", [string]"black")
[void]$dt1.Rows.Add([string]"id 3", [string]"fact 3", [string]"green")
[void]$dt1.Rows.Add([string]"id 3", [string]"fact 4", [string]"yellow")
[void]$dt1.Rows.Add([string]"id 4", [string]"fact 1", [string]"purple")
[void]$dt1.Rows.Add([string]"id 4", [string]"fact 2", [string]"yellow")
[void]$dt1.Rows.Add([string]"id 4", [string]"fact 3", [string]"purple")
[void]$dt1.Rows.Add([string]"id 4", [string]"fact 4", [string]"yellow")
# Write the data to a xml and data file:
$dt1.WriteXmlSchema('.\CacheSchema.xsd')
$dt1.WriteXml(".\CacheData.xml")
##############################
# End of this script #
# Going on with the next one #
##############################
# Set the search criteria:
$column1 = ""
$column2 = "fact 1"
$column3 = "purple"
# build the query:
$qquery1 = "
datacolumnone like '%"+$column1+"%'
AND datacolumntwo like '%"+$column2+"%'
AND datacolumnthree like '%"+$column3+"%'
";
# Create the Datatable:
$dt2 = New-Object System.Data.Datatable
# Load the data:
[void]$dt2.ReadXmlSchema(".\CacheSchema.xsd")
[void]$dt2.ReadXml(".\CacheData.xml")
# Run the query:
$dt2.Select($qquery1)
现在我想要实现的是:
- 从具有以下值的行中查找所有ID(datacolumnone):" fact 1" in" datacolumntwo"和"紫色"在" datacolumnthree"
- 然后进一步过滤id" datacolumnone"在上面的查询结果中," datacolumntwo"是"事实4"和 " datacolumnthree"是"黄"
- 然后,当我们有过滤后的ID时,显示在" datacolumnone"中的行。来自上面和之后的过滤查询的id " datacolumntwo"价值"事实2"
醇>
https://msdn.microsoft.com/en-us/library/system.data.datatable.select%28v=vs.110%29.aspx