我有3个字符串列表。
List1 - Student Name List2 - Student School List3 - Student Location Student 1 Student 1 School Student 1 Location Student 2 Student 2 School Student 2 Location Student 3 Student 3 School Student 3 Location Student 4 Student 4 School Student 4 Location Student 5 Student 5 School Student 5 Location
结构class
StudentDetails
我想将前3个列表设为Public Structure StudentDetails()
Public StudentName As String
Public StudentSchool As String
Public StudentLocation As String
End Structure
我使用以下代码执行此操作
List of StudentDetails
使用Dim StudentDetailsList As New List(Of StudentDetails)
For i = 0 to List1.Count - 1
Dim StudentDetail As New StudentDetail
With StudentDetail
.StudentName = List1(i)
.StudentSchool = List2(i)
.StudentLocation = List3(i)
End With
StudentDetailsList.Add(StudentDetail)
Next
或其他方法有更好的方法吗?
答案 0 :(得分:1)
有很多方法可以做到这一点,有些方法比其他方法更容易阅读。
首先,我会将StudentDetails
作为一个类而不是一个结构(例如,参见When should I use a struct instead of a class?。
既然你有一个类,你可以给它一个带有参数的New构造函数,如第三个例子所示:
Option Infer On
Option Strict On
Module Module1
Public Class StudentDetails
Public Name As String
Public School As String
Public Location As String
Public Sub New()
' empty constuctor
End Sub
Public Sub New(name As String, school As String, location As String)
Me.Name = name
Me.School = school
Me.Location = location
End Sub
' make it easy to represent StudentDetails as a string...
Public Overrides Function ToString() As String
Return $"{Me.Name} {Me.School} {Me.Location}"
End Function
End Class
Sub Main()
Dim list1 As New List(Of String) From {"Adam", "Betty", "Charles", "Wilma"}
Dim list2 As New List(Of String) From {"Ace", "Best", "Classy", "Wacky"}
Dim list3 As New List(Of String) From {"Attic", "Basement", "Cellar", "Windowledge"}
' a not-very tidy example using Zip:
Dim StudentDetailsList = list1.Zip(list2, Function(a, b) New With {.name = a, .school = b}).Zip(list3, Function(c, d) New StudentDetails With {.Name = c.name, .School = c.school, .Location = d}).ToList()
' one way of writing out the StudentDetailsList...
For Each s In StudentDetailsList
Console.WriteLine(s.ToString())
Next
StudentDetailsList.Clear()
' a bit cleaner using a loop:
For i = 0 To list1.Count() - 1
StudentDetailsList.Add(New StudentDetails With {
.Name = list1(i),
.School = list2(i),
.Location = list3(i)})
Next
' another way of writing out the StudentDetailsList...
Console.WriteLine(String.Join(vbCrLf, StudentDetailsList))
StudentDetailsList.Clear()
' easy to write with a New constructor, but not necessarily as easy to read as the previous example:
For i = 0 To list1.Count() - 1
StudentDetailsList.Add(New StudentDetails(list1(i), list2(i), list3(i)))
Next
Console.WriteLine(String.Join(vbCrLf, StudentDetailsList))
Console.ReadLine()
End Sub
End Module
我在$
方法中使用了.ToString()
字符串格式化程序:它是在VS2015中引入的,所以如果您使用的是早期版本,则可以使用String.Format("{0} {1} {2}", Me.Name, Me.School, Me.Location)
代替。
作为关于命名StudentDetails
的属性的注释,"学生"在StudentName
中,StudentSchool
和StudentLocation
是多余的。
答案 1 :(得分:0)
好吧,您可以使用PKCS#7 padding扩展方法的重载,通过合并元素的索引,将列表中的每个元素投影到新的StudentDetail
中。假设三个列表具有相同数量的元素,您可以执行以下操作:
// using C#
var result=List1.Select((e,i))=>new StudentDetail
{
StudentName =e,
StudentSchool = List2[i],
StudentLocation = List3[i]
}).ToList();
我认为在Vb中会有(对不起,我是一名c#程序员):
Dim StudentDetailsList=List1.Select(Function(e, i) _
New StudentDetail
With StudentDetail
.StudentName = e
.StudentSchool = List2(i)
.StudentLocation = List3(i)
End With).ToList();
但是使用for
并不是一个糟糕的解决方案,在许多情况下更具可读性。