如何在PowerShell中比较两个DataRow对象数组?

时间:2021-07-10 04:48:22

I have two arrays of System.Data.DataRow objects which I want to compare.

我有两个System.Data.DataRow对象数组,我想比较。

The rows have two columns A and B. Column A is a key and I want to find out which rows have had their B column changed and which rows have been added or deleted.

行有两列A和B.列A是一个键,我想找出哪些行的B列已更改,哪些行已添加或删除。

How do I do this in PowerShell?

我如何在PowerShell中执行此操作?

3 个解决方案

#1


4  

I wrote a script to do this a little while back. The script (Compare-QueryResults.ps1) is available here and you will also need my Run-SQLQuery script (available here) or you can replace that with a script or function of your own.

我写了一个脚本来做这件事一会儿。脚本(Compare-QueryResults.ps1)在这里可用,您还需要我的Run-SQLQuery脚本(可在此处获得),或者您可以使用您自己的脚本或函数替换它。

Basically, what the script does is take the results of each of your queries and break the datarows apart so that each field is its own object. It then uses Compare-Object to check for any differences between the data in those rows. It returns a comparison object that shows you all the differences between the data returned.

基本上,脚本所做的是获取每个查询的结果并将数据行分开,以便每个字段都是它自己的对象。然后,它使用Compare-Object检查这些行中的数据之间的任何差异。它返回一个比较对象,显示返回数据之间的所有差异。

The results are an object, so you can save them to a variable and use Sort-Object or the Format-* cmdlets with them.

结果是一个对象,因此您可以将它们保存到变量中,并使用Sort-Object或Format- * cmdlet。

Good luck. If you have any problems with the scripts, let me know, I'd be happy to walk you through them. I've been using them for application testing, seeing what rows are being modified by different actions in a program.

祝好运。如果你对脚本有任何问题,请告诉我,我很乐意引导你完成它们。我一直在使用它们进行应用程序测试,看看程序中的不同操作正在修改哪些行。

#2


1  

To simply compare two System.Data.DataRow, you can do something like this:

要简单地比较两个System.Data.DataRow,您可以执行以下操作:

foreach ($property in ($row1 | Get-Member -MemberType Property)) {
    $pName = $property.Name

    if ($row1.$pName -ne $row2.$pName) {
        Write-Host "== $pName =="
        $row1.$pName
        $row2.$pName
    }
}

#3


0  

Do you need two arrays of DataRows? the DataRow object has a RowState property which will give you what you require. See the MSDN Docs: http://msdn.microsoft.com/

您需要两个DataRows数组吗? DataRow对象具有RowState属性,可以为您提供所需的属性。请参阅MSDN文档:http://msdn.microsoft.com/

#1


4  

I wrote a script to do this a little while back. The script (Compare-QueryResults.ps1) is available here and you will also need my Run-SQLQuery script (available here) or you can replace that with a script or function of your own.

我写了一个脚本来做这件事一会儿。脚本(Compare-QueryResults.ps1)在这里可用,您还需要我的Run-SQLQuery脚本(可在此处获得),或者您可以使用您自己的脚本或函数替换它。

Basically, what the script does is take the results of each of your queries and break the datarows apart so that each field is its own object. It then uses Compare-Object to check for any differences between the data in those rows. It returns a comparison object that shows you all the differences between the data returned.

基本上,脚本所做的是获取每个查询的结果并将数据行分开,以便每个字段都是它自己的对象。然后,它使用Compare-Object检查这些行中的数据之间的任何差异。它返回一个比较对象,显示返回数据之间的所有差异。

The results are an object, so you can save them to a variable and use Sort-Object or the Format-* cmdlets with them.

结果是一个对象,因此您可以将它们保存到变量中,并使用Sort-Object或Format- * cmdlet。

Good luck. If you have any problems with the scripts, let me know, I'd be happy to walk you through them. I've been using them for application testing, seeing what rows are being modified by different actions in a program.

祝好运。如果你对脚本有任何问题,请告诉我,我很乐意引导你完成它们。我一直在使用它们进行应用程序测试,看看程序中的不同操作正在修改哪些行。

#2


1  

To simply compare two System.Data.DataRow, you can do something like this:

要简单地比较两个System.Data.DataRow,您可以执行以下操作:

foreach ($property in ($row1 | Get-Member -MemberType Property)) {
    $pName = $property.Name

    if ($row1.$pName -ne $row2.$pName) {
        Write-Host "== $pName =="
        $row1.$pName
        $row2.$pName
    }
}

#3


0  

Do you need two arrays of DataRows? the DataRow object has a RowState property which will give you what you require. See the MSDN Docs: http://msdn.microsoft.com/

您需要两个DataRows数组吗? DataRow对象具有RowState属性,可以为您提供所需的属性。请参阅MSDN文档:http://msdn.microsoft.com/