Reputation: 15807
Please see the code below, which was written by someone else and works very well:
Public Function GetMembers(Optional ByVal sortExpression As String = "MemberId ASC") As List(Of Member) Implements IMemberDao.GetMembers
Dim sql As String =
" SELECT MemberId, Email, CompanyName, City, Country" &
" FROM [Member] ".OrderBy(sortExpression)
Return db.Read(sql, Make).ToList()
End Function
Public Iterator Function Read(Of T)(ByVal sql As String, ByVal make As Func(Of IDataReader, T), ParamArray ByVal parms() As Object) As IEnumerable(Of T)
Using connection = CreateConnection()
Using command = CreateCommand(sql, connection, parms)
Using reader = command.ExecuteReader()
Do While reader.Read()
Yield make(reader)
Loop
End Using
End Using
End Using
End Function
Private Shared Make As Func(Of IDataReader, Member) =
Function(reader) _
New Member() With {
.MemberId = Extensions.AsId(reader("MemberId")),
.Email = Extensions.AsString(reader("Email")),
.CompanyName = Extensions.AsString(reader("CompanyName")),
.City = Extensions.AsString(reader("City")),
.Country = Extensions.AsString(reader("Country"))
}
I understand that Make is a delegate that populates the Member objects with values, but I do not understand how a list of Persons is returned by the Read function? (a list is returned and works very well).
Upvotes: 0
Views: 53
Reputation: 5822
it does this by the yield keyword. This is generally how it works when you are iterating over a collection. Take a look here:
http://msdn.microsoft.com/en-us/library/vstudio/hh156729.aspx
This should give you a good/simple understanding.
Upvotes: 2