【问题标题】:.NET Split Arraylist based on values.NET 根据值拆分 Arraylist
【发布时间】:2015-03-05 22:38:10
【问题描述】:

我有一个具有这种结构的书籍的数据网格视图: 网格有 3 列出版物、作者和标题。 该网格中可能有重复的记录。 数据中有 5 个出版物。

我想根据发布拆分数据,因此将值分配给 5 个变量所以我首先将所有值排序并添加到数组列表中,但我不知道如何拆分它们。

Structure abb
    Public publications As String
    Public author As String
    Public title As String
End Structure

Public arlist As New ArrayList
Public thebooks As New abb

For i As Integer = 0 To Databook.Rows.Count - 2  'databook is the gridview
    With thebooks 
        .publications = CStr(Databook.Rows(i).Cells(1).Value)
        .author = CStr(Databook.Rows(i).Cells(2).Value)
        .title = CStr(Databook.Rows(i).Cells(3).Value)
    End With
    If Not arlist .Contains(thebooks) Then
        arlist .Add(thebooks)
    End If
Next i

现在我创建了数组列表,但在该列表中有 5 个出版物,其中包含一本书或多本书。 我想有 5 个基于发布名称的数组列表(末尾有 5 个变量)。

我可能会走错第一步,因为我猜首先从 datagridview 行中还有另一种方法可以做到这一点...。

注意:VB 或 C# 的答案可以帮助我找到自己的方式。

【问题讨论】:

  • 您的ArrayList 不是List(Of abb) 的任何原因?这将通过消除对这些结构进行强制转换/拆箱的需要来简化拆分它的代码。
  • 没有理由。我尽我所能让它发挥作用。

标签: c# arrays vb.net visual-studio arraylist


【解决方案1】:

我仍然不知道你想要什么,你的意思是“出版物”可以有这样的价值观:

abb.publications = "书 A、书 B、书 C"?

如果是,你想用它们做什么?或者您只是想在出版物中按书名对 abb 进行分组?像这样的东西? (使用LINQ

首先,我认为您可以按照 Simon 的建议将 Public arlist As New ArrayList 更改为 Public arlist As New List<abb>。然后在你的 For:

Dim books = abb.SelectMany(Function(b) b.Split(",")).Distinct().ToArray();
Dim abbByBookName = books.Select(Function(b) arlist.Where(Function(a) a.publications.Contains(b))).ToArray()

【讨论】:

    猜你喜欢
    • 2014-12-29
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 2021-01-17
    • 1970-01-01
    相关资源
    最近更新 更多