如何从vb.net中的ienumerable函数返回

时间:2015-07-18 09:57:51

标签: vb.net

Public Shared Function GetDataSet() As IEnumerable(Of SerialData)

    Dim sCon As New SQLConnect
    Dim strsql As String
    Dim p As New Control
    sCon.sqlAdp = New SqlDataAdapter

    strsql = "select serialid," & _
                " serialno," & _
                " serialdesc," & _
                " b.materialname," & _
                " b.drawing," & _
                " a.workorder," & _
                " isnull((select top 1 patno from patternmaster where patid = a.patid),'Not Defined') as patno," & _
                " case when a.activeflag = 1 then 'True' else 'False' end as activeflag" & _
                " from serialmaster a," & _
                " materialmaster b" & _
                " where 1 = 1" & _
                " and a.materialid = b.materialid" & _
                " and b.activeflag = 1"

    sCon.sqlCmd.CommandText = strsql
    sCon.sqlAdp.SelectCommand = sCon.sqlCmd
    sCon.sqlAdp.Fill(sCon.DS, "Listing")

    Dim dtTable As DataTable
    dtTable = sCon.DS.Tables("Listing")

    ' For Each row As DataRow In dtTable.Rows
    '  Return dtTable.AsEnumerable().[Select](Function(row) New With { _
    '    Key .serialid = row("serialid"), _
    '    Key .serialno = row("serialno"), _
    '    Key .serialdesc = row("serialdesc"), _
    '    Key .materialname = row("materialname"), _
    '    Key .drawing = row("drawing"), _
    '    Key .workorder = row("workorder"), _
    '    Key .patno = row("patno") _
    '})
    ' Next


    For Each row As DataRow In dtTable.Rows
        p.serialid.Add(row("serialid"))
        p.serialno.Add(row("serialno"))
        p.serialdesc.Add(row("serialdesc"))
        p.materialname.Add(row("materialname"))
        p.drawing.Add(row("drawing"))
        p.workorder.Add(row("workorder"))
        p.patno.Add(row("patno"))
    Next

    Return p
End Function

控制类:

Public Class Control

   Public serialid As New Generic.List(Of String)
   Public serialno As New Generic.List(Of String)
   Public serialdesc As New Generic.List(Of String)
   Public materialname As New Generic.List(Of String)
   Public drawing As New Generic.List(Of String)
   Public workorder As New Generic.List(Of String)
   Public patno As New Generic.List(Of String)
   Public result As New Generic.List(Of String)

End Class

ServerSidePro类:

Public Class ServerSidePro
Implements System.Web.IHttpHandler

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    Dim d As New Control
    ' Those parameters are sent by the plugin
    Dim iDisplayLength = Integer.Parse(context.Request("iDisplayLength"))
    Dim iDisplayStart = Integer.Parse(context.Request("iDisplayStart"))
    Dim iSortCol = Integer.Parse(context.Request("iSortCol_0"))
    Dim iSortDir = context.Request("sSortDir_0")

    'Fetch the data from a repository (in my case in-memory)

    'Dim p = SerialData.GetDataSet()
    Dim p = DirectCast(SerialData.GetDataSet(), IEnumerable(Of SerialData))
    ' prepare an anonymous object for JSON serialization

    Dim aaData2 = p.select(Function(h) New With {h.serialid, h.serialno, h.serialdesc, _
                                                  h.materialname, h.drawing, h.workorder, h.patno}).Skip(iDisplayStart).Take(iDisplayLength)

    Dim str As New List(Of String())

    For Each item In aaData2
        Dim arr As String() = New String(6) {item.serialid, item.serialno, item.serialdesc, _
                                                  item.materialname, item.drawing, item.workorder, item.patno}

        str.Add(arr)
    Next

    Dim result = New With { _
     Key .iTotalRecords = p.Count(), _
     Key .iTotalDisplayRecords = p.Count(), _
     Key .aaData = str
     }
    Dim serializer As New JavaScriptSerializer()
    Dim json = serializer.Serialize(result)
    context.Response.ContentType = "application/json"
    context.Response.ClearHeaders()
    context.Response.Write(json)
    context.Response.End()
    'Return d

End Sub

ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
    Get
        Return False
    End Get
End Property

End Class

如何从此函数返回数据集它给出错误无法将类型为'Control'的对象强制转换为'System.Collections.Generic.IEnumerable'。

1 个答案:

答案 0 :(得分:1)

最简单的方法是使用List()对象。我会重组你的课程

Public Class Control
    Public LControl As New List(Of Control)

    Public serialid As String
    Public serialno As String
    Public serialdesc As String
    Public materialname As String
    Public drawing As String
    Public workorder As String
    Public patno As String
    Public result As String

End Class
​