我正在使用这个示例VB代码,使用MS Web API将一组数据行返回到html页面中的表 - 而且,它运行良好。
Public Class CalendarEventsController
Inherits ApiController
Private CalendarEvents As CalendarEvent() = New CalendarEvent() {
New CalendarEvent() With {
.id = 1,
.eventName = "Event 1"
}, New CalendarEvent() With {
.id = 2,
.eventName = "Event 2"
}}
' GET api/<controller>
Public Function GetCalendarEvents() As IEnumerable(Of CalendarEvent)
Return CalendarEvents
End Function
End Class
问题是我需要使用来自数据库的数据填充此列表。我需要某种类型的For Each循环来遍历数据库行,然后在控制器列表中创建相应的行。我似乎无法构建这样的循环。只要让i = 1到i = 2就可以了。
编辑:工作版本:
Public Class CalendarEventsController
Inherits ApiController
Public Function GetCalendarEvents() As IEnumerable(Of EventCalendar)
Dim myContext As New RoutesEntities()
Return From eventlist In myContext.EventCalendars Select eventlist
End Function
End Class