【问题标题】:convert one array object to IEnumerable将一个数组对象转换为 IEnumerable
【发布时间】:2013-07-04 23:28:03
【问题描述】:

我想比较两个datagridviews,并在IEnumerable接口上使用Except方法,以了解它们之间的区别。 我的数据网格视图的一个示例:

DG1 idProduct 项目 1 项 A 1 项 B 2 项目 C 2 项目 D DG2 idProduct 项目价格 IdSupplier 1 项目 A 10.00 1 1 项目 B 20.00 1 2 项目 C 30.00 1 2 项目 D 40.00 1 1 项目 A 20.00 3 1 项目 B 30.00 3 2 C 项 40.00 3 2 项目 D 50.00 3

所以,我试图将来自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

所以,我尝试了IEnumerable.Except,但我的itemArray()似乎是一个对象,因为我在尝试投射exceptItems时收到消息"System.linq.Enumerable+<ExceptIterator>d_99'1[System.Object]",如下:

Dim exceptItems = itemListArray.Except(itemArray(2))

我也试过了:

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())

并且知道我弄错了。第 13 号。我认为问题在于我必须将 itemArray() 转换为 IEnumerable,有什么方法可以在不对代码进行重大更改的情况下做到这一点?

【问题讨论】:

    标签: arrays vb.net list except


    【解决方案1】:

    由于您没有option strict,编译器不知道您的itemArray() 是什么类型,因此它使用对象的最低公分母。

    您应该打开选项严格,然后修复错误。您可以在项目属性中或使用Options Strict 在类的顶部执行此操作。不使用严格是从 vb6 天开始的,确实有助于避免这样的问题。它将迫使您将变量定义为应有的值。

    【讨论】:

    • 天哪!谢谢!我打开这个东西,看到了很多工作......我会再次将其关闭......
    【解决方案2】:

    Except 仅在IEnumerable(Of T) 接口上,不在原版IEnumerable 上。

    IEnumerable(Of T) 是在普通数组上实现的,因此您不必重复加载数据。

    另外,由于Except是扩展方法,所以需要导入System.Linq。

    【讨论】:

    • 您好,谢谢!那么,有没有办法将 system.string 对象转换为 system.IEnumerable(of T)?
    • 嗯,字符串是 IEnumerable(of Char)...但这似乎不是您需要的...
    • 您好,谢谢!我看到,如果我实现 IEnumerable,我将得到一个 IEnumerable(Char)。为了防止这种情况,我也许应该使用列表或 Arraylist,并避免使用 .toarray 方法?没关系!由于将它用于我的 dgv1 列表值没有问题,我如何使用它们来存储我的 dgv2 中的值?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 2012-06-02
    • 2018-08-27
    相关资源
    最近更新 更多