2016-04-27 14 views
0

我有3個字符串列表。使用VB.NET將多個字符串列表合併到對象列表中

 
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 

而且結構StudentDetails

Public Structure StudentDetails() 
    Public StudentName As String 
    Public StudentSchool As String 
    Public StudentLocation As String 
End Structure 

我要讓前3名單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 

有沒有更好的使用Linq或其他方法來做到這一點?

回答

0

那麼,您可以使用Select擴展方法的重載,通過合併元素的索引,將其中一個列表的每個元素投影到新的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不是一個不好的解決方案,在許多情況下更具可讀性。

1

有很多方法可以做到這一點,比其他人更容易閱讀。

首先,我會做StudentDetails一類,而不是一個結構(例如,參見When should I use a struct instead of a class?

現在你有一個類,你可以給它一個帶參數的新構造,如在第三例中使用在這裏:

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,StudentSchoolStudentLocation中的「學生」是多餘的。

+0

這只是一個示例數據。真實數據不是學生的細節。但是,我會記下你的提示,並感謝你解釋爲什麼我應該使用類而不是結構。 – Ian

相關問題