【问题标题】:How to convert dataset object to List<string> in vb.net如何在 vb.net 中将数据集对象转换为 List<string>
【发布时间】:2018-05-04 23:28:32
【问题描述】:

我正在从数据库返回一组结果行,我想在 UI 上为 HTML 控件分配这些值。

我正在从 DB 中将数据提取到 DataSet 中。

例如,我的数据如下所示:

ID  EmpId    Question    Comments

1    2        abcdefgh   comments1
2    6        xyhgjkjh   comments2
3    6        kjhkjhjk   comments3
4    6        uyyiuyuui  comments4
5    3        erteyeyuy  comments5
6    6        qapooioip  comments6

基于输入(EmpId)我获取数据。例如 EmpId = 6,我想使用 HTML 字段在 UI 上显示上述数据,如下所示:

第一季度。 xyhgjkjh

cmets2

第二季度。 kjhkjhjk

cmets3

Q3.uyyiuyuui

cmets4

Q4.qapooioip

cmets6

我只想通过循环访问数据库中的结果集来显示 HTML 标签。

我正在创建如下属性:

Public class MyClass
{
   public int Id{get;set;}
   public int EmpId{get;set;}
   public String Question{get;set;}
   public String Comments{get;set;}
}

我想使用上面的类并将结果集值设置为属性并循环遍历它并分配给 UI 上的 HTML 控件。

如何转换数据集并将其设置为 MyClass obj 属性并将其分配给 VB.NET 中的 HTML 控件。

我需要 List for Question 属性吗?

有什么帮助吗?提前致谢。

【问题讨论】:

  • 我真的很难理解你的问题。你的实际问题是什么?将DataSet 转换为您的班级代表?在网页上显示MyClass 对象中结构化的数据?过滤数据?所有的?您使用的是什么网络技术?网络表单? MVC?剃须刀?
  • 它是一个 asp.net 网络表单。我现在有一个数据集,我想循环数据集并将每个值分配给我的类对象(可能是列表)

标签: vb.net list


【解决方案1】:

使用你的类:

' I'm going to assume that the class properties are in the same order as the dataset and that there is only one table in the dataset.

Dim TheClassInstance as MyClass ' Create an instance of the class
Dim MyClassList as List(Of TheClassInstance)
Dim MyDataRow as DataRow
For Each MyDataRow in TheDataSet.Tables(0).Rows
     TheClassInstance = New MyClass 
     TheClassInstance.Id = MyDataRow(0)
     TheClassInstance.EmpId = MyDataRow(1)
     TheClassInstance.Question = MyDataRow(2)
     TheClassInstance.Comments = MyDataRow(3)
     MyClassList.Add(TheClassInstance)
     ' Everytime you loop you're adding an instance of the class to the list
Next

要访问类的每个实例,您可以使用 for each 循环或使用变量作为索引的循环进行循环,以获取列表中的数据。使用 for each 循环:

Dim MyClassInstance as new MyClass
For Each MyClassInstance in MyClassList 
    ' You can set the text property of labels and textbox controls to the data
    Textbox_Comments.text = MyClassInstance.Comments
    ' ... etc
Next

已编辑以解决每位家长的多个问题:

改变你的班级:

Public class MyClass

   Public Property Id as Integer
   Public Property EmpId as Integer
   Public Property Question as List(of String)
   Public Property Comments as String

End Class

我假设每个 EmplID 都有一个包含 EmplId 的父表和一个 Questions 子表:

Dim TheClassInstance as MyClass ' Create an instance of the class
Dim MyDataRow as DataRow
Dim MyDataRow2 as DataRow ' For clarity
For Each MyDataRow IN ParentDataSet.Tables(0).Rows
     TheClassInstance = New MyClass 
     TheClassInstance.Id = MyDataRow(0)
     TheClassInstance.EmpId = MyDataRow(1)
     TheClassInstance.Comments = MyDataRow(3)
     Dim MySecondDataSet as Dataset = GetQuestions(MyDataRow(1) ' Get questions for EmplId
     For Each MyDataRow2 in MySecondDataSet.Tables(0).Rows
         ' It's assumed that the question is the only column in the table
         TheClassInstance.Questions.Add(MyDataRow2(0))
     Next
Next

当所有问题都在表格中的一行时:

Dim TheClassInstance as MyClass ' Create an instance of the class
Dim MyDataRow as DataRow
For Each MyDataRow IN ParentDataSet.Tables(0).Rows
     TheClassInstance = New MyClass 
     TheClassInstance.Id = MyDataRow(0)
     TheClassInstance.EmpId = MyDataRow(1)
     TheClassInstance.Comments = MyDataRow(3)
     ' I assume that the questions start in column 4
     ' Add each question in the row, I assume 4 questions in order
     TheClassInstance.Questions.Add(MyDataRow(4))
     TheClassInstance.Questions.Add(MyDataRow(5))
     TheClassInstance.Questions.Add(MyDataRow(6))
     TheClassInstance.Questions.Add(MyDataRow(7))
Next

【讨论】:

  • 实例化应该在循环的开始,而不是结束,否则你会在第一次迭代中得到NullReferenceException
  • 在循环之前在循环外有一个实例化所以没关系...
  • 好的,现在有了。我仍然认为你不应该实例化比你需要的更多的对象(最后一个对象永远不会被使用,只会占用内存)。
  • 好吧……让他开始……无论如何,只要他离开程序或代码块,它就消失了……但是有一个手动清理垃圾的论点……哈哈...
  • 不,他一离开程序就没有了。这是 VB,不是 C。
【解决方案2】:

创建将DataRow 转换为您的类MyClass 的方法的另一种方法

Public Function ToMyClass(row As DataRow)
    Return New MyClass With
    {
        .Id = row.Field(Of Integer)("ID"),
        .EmpId = row.Field(Of Integer)("EmpID"),
        .Question = row.Field(Of String)("Question"),
        .Comments = row.Field(Of String)("Comments"),
    }
End Function

通用方法DataRow.Field(Of T) 会将列值转换为预期类型,这提供了编译器提供的某种类型安全性 - 为此您需要将Option Strict 设置为On

然后您将能够在“一行”中创建您的课程列表

Dim data As DataTable = dataset.Tables(0)
Dim questions As List(Of MyClass) = 
    data.AsEnumerable().Select(AddressOf ToMyClass).ToList()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 2022-01-05
    • 2020-01-11
    相关资源
    最近更新 更多