I want to compare two datagridviews, and use the Except
method on the IEnumerable
interface, in order to know the difference among them. One example of my datagridviews:
我想比较两个datagridviews,并在IEnumerable接口上使用Except方法,以便了解它们之间的区别。我的datagridviews的一个例子:
DG1 idProduct Item 1 Item A 1 Item B 2 Item C 2 Item D DG2 idProduct Item Price IdSupplier 1 Item A 10.00 1 1 Item B 20.00 1 2 Item C 30.00 1 2 Item D 40.00 1 1 Item A 20.00 3 1 Item B 30.00 3 2 Item C 40.00 3 2 Item D 50.00 3
So, I have tried to put the data from dgv1
into an array, and the data from dgv2
into an dynamic array, cause I want a list for each IdSupplier
(in case, 1, 3) and compare them with the except method. My code is:
所以,我试图将dgv1中的数据放入一个数组中,并将dgv2中的数据放入一个动态数组中,因为我需要每个IdSupplier的列表(如果是1,3),并将它们与except方法进行比较。我的代码是:
Imports System.Data.SqlClient
Imports System.Data
Imports datagridviewTota.DataSet1TableAdapters
Imports System.Collections.Generic
Imports System.Collections.ArrayList
Imports System.Collections.CollectionBase
Public Class form1
Public itemArray()
Public ItemListArray()
Public Shared j As Integer
Private sub main ()
(…)
Dim ds1 As New DataSet
Dim item As List(Of String) = New List(Of String)
Dim Id As Integer
Dim dr As DataRow
Dim dr1 As DataRow
Dim itemList As List(Of String) = New List(Of String)
Dim idSuppliers () as integer
ReDim itemArray(j) ‘ j represents the numbers of elements in idSuppliers() (third column of dg2)
Try
//create an array for the dgv1//
For Each row As DataGridViewRow In dgv1.Rows
item = dgv1.Rows(row.Index).Cells(1).Value
itemList.Add(item)
Next
Dim itemListArray = itemList.toArray()
//create a dynamic array for the dgv2 by filtering for each idSuppliers, put the values from the second column into a list and convert each list into a dynamic array//
For Each element In idSuppliers
Dim dv As New DataView()
dv = New DataView(ds1.Tables("idProduct"))
With dv
.RowFilter = "idSupplier = " & element & " "
End With
dgv2.DataSource = dv
For Each row As DataGridViewRow In dgv2.Rows
Id = dgv2.Rows(row.Index).Cells(3).Value
If Id = element Then
item = dgv2.Rows(row.Index).Cells(1).Value
itemList.Add(item)
End If
Next
itemArray(i) = itemList.ToArray()
itemList.clear()
i = i + 1
Next
end sub
So, I have tried the IEnumerable.Except
, but it seems that my itemArray()
is a object, cause I got the message "System.linq.Enumerable+<ExceptIterator>d_99'1[System.Object]"
, when I try to try to cast exceptItems
, as follows:
所以,我已经尝试了IEnumerable.Except,但似乎我的itemArray()是一个对象,因为我得到了消息“System.linq.Enumerable +
Dim exceptItems = itemListArray.Except(itemArray(2))
I also have tried:
我也尝试过:
Dim onlyInFirstSet As IEnumerable(Of String) = itemListArray.Except(itemArray(2))
Dim output As New System.Text.StringBuilder
For Each str As String In onlyInFirstSet
output.AppendLine(str)
Next
MsgBox(output.ToString())
And know I get the err. number 13. I think the problem is that I have to convert itemArray()
into an IEnumerable
, is there any way that I could do this without major changes in my code?
并知道我得到了错误。数字13.我认为问题是我必须将itemArray()转换为IEnumerable,有没有什么方法可以在我的代码中没有重大更改的情况下执行此操作?
2 个解决方案
#1
1
Since you don't have option strict
, the compiler doesn't know what type your itemArray()
is so it uses the lowest common denominator of object.
由于您没有选项strict,编译器不知道您的itemArray()是什么类型,因此它使用对象的最小公分母。
You should turn option strict on and then fix the errors. You can do it either in the project properties or at the top of the class using Options Strict
. Not using strict is a hold over from vb6 days and really helps to avoid problems like this. It will force you to define your variables as what they should be.
您应该打开选项严格,然后修复错误。您可以使用Options Strict在项目属性中或在类的顶部执行此操作。不使用严格是从vb6天暂停,真的有助于避免这样的问题。它会强制您将变量定义为它们应该是什么。
#2
1
Except
is only on the IEnumerable(Of T)
interface, not on the vanilla IEnumerable
.
除了仅在IEnumerable(Of T)接口上,不在vanilla IEnumerable上。
IEnumerable(Of T)
is implemented on a plain array, so you don't have to double-load your data.
IEnumerable(Of T)是在普通数组上实现的,因此您不必双重加载数据。
Also, since Except
is an extension method, you need to import System.Linq
.
此外,由于Except是一种扩展方法,因此您需要导入System.Linq。
#1
1
Since you don't have option strict
, the compiler doesn't know what type your itemArray()
is so it uses the lowest common denominator of object.
由于您没有选项strict,编译器不知道您的itemArray()是什么类型,因此它使用对象的最小公分母。
You should turn option strict on and then fix the errors. You can do it either in the project properties or at the top of the class using Options Strict
. Not using strict is a hold over from vb6 days and really helps to avoid problems like this. It will force you to define your variables as what they should be.
您应该打开选项严格,然后修复错误。您可以使用Options Strict在项目属性中或在类的顶部执行此操作。不使用严格是从vb6天暂停,真的有助于避免这样的问题。它会强制您将变量定义为它们应该是什么。
#2
1
Except
is only on the IEnumerable(Of T)
interface, not on the vanilla IEnumerable
.
除了仅在IEnumerable(Of T)接口上,不在vanilla IEnumerable上。
IEnumerable(Of T)
is implemented on a plain array, so you don't have to double-load your data.
IEnumerable(Of T)是在普通数组上实现的,因此您不必双重加载数据。
Also, since Except
is an extension method, you need to import System.Linq
.
此外,由于Except是一种扩展方法,因此您需要导入System.Linq。