2016-04-27 6 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 

私はこの

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 

を行うには、次のコードを使用しているList of StudentDetails

に最初の3のリストを作りたいですそこには良いですか? Linqまたは他の方法を使用してこれを行う方法?

答えて

0

また、Select拡張メソッドのオーバーロードを使用して、要素のインデックスを組み込むことによって、リストの1つの要素を新しいStudentDetailに投影することができます。 3つのリストは、要素の同じ量を持っていると仮定すると、次の操作を実行できます

// using C# 
var result=List1.Select((e,i))=>new StudentDetail 
            { 
             StudentName =e, 
             StudentSchool = List2[i], 
             StudentLocation = List3[i] 
            }).ToList(); 

は、私がVBで思うだろうこと(申し訳ありませんが、私は交流#プログラマ):

Dim StudentDetailsList=List1.Select(Function(e, i) _ 
             New StudentDetail 
             With StudentDetail 
              .StudentName = e 
              .StudentSchool = List2(i) 
              .StudentLocation = List3(i) 
             End With).ToList(); 

しかし、使用してforは悪い解決策ではありません。多くの場合、読みやすくなります。

1

これを行うには多くの方法がありますが、他の方法よりも読みやすいものがあります。

まず、私は、例えば、When should I use a struct instead of a class?を参照してください(StudentDetailsクラスの代わりの構造になるだろう。

第三の例で使用されるように今、あなたがクラスを持っていることを、あなたは、それをパラメータで新しいコンストラクタを与えることができますここではそれはVS2015で導入されましたので、あなたは、あなたが代わりにString.Format("{0} {1} {2}", Me.Name, Me.School, Me.Location)を使用することができます以前のバージョンを使用している場合

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()方法で$文字列フォーマッタを使用しました。

StudentDetailsという名前の注記として、StudentName,StudentSchoolおよびStudentLocationの「学生」は冗長です。

+0

これは単なるサンプルデータです。実際のデータは生徒の詳細ではありません。しかし、私はあなたのヒントを書き留めておき、構造の代わりにクラスを使うべき理由を説明してくれてありがとう。 – Ian

関連する問題