2010-05-17 131 views
0

我有一些代碼嘗試循環LINQ結果,但它似乎並沒有工作。如何通過LINQ結果循環(VB.NET)

下面的代碼

Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest 
     ''# the page contenttype is plain text' 
     HttpContext.Current.Response.ContentType = "text/plain" 

     ''# store the querystring as a variable' 
     Dim qs As Nullable(Of Integer) = Integer.TryParse(HttpContext.Current.Request.QueryString("ID"), Nothing) 

     ''# use the RegionsDataContext' 
     Using RegionDC As New DAL.RegionsDataContext 

      ''# create a (q)uery variable' 
      Dim q As Object 

      ''# if the querystring PID is not blank' 
      ''# then we want to return results based on the PID' 
      If Not qs Is Nothing Then 
       ''# that fit within the Parent ID' 
       q = (From r In RegionDC.bt_Regions _ 
         Where r.PID = qs _ 
         Select r.Region).ToArray 

       ''# now we loop through the array' 
       ''# and write out the ressults' 
       For Each item As DAL.bt_Region In q 
        HttpContext.Current.Response.Write(item.Region & vbCrLf) 
       Next 

      End If 



     End Using 
    End Sub 

這裏的錯誤

Public member 'Region' on type 'String' not found. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MissingMemberException: Public member 'Region' on type 'String' not found.

Source Error:

Line 33: ' and write out the ressults Line 34:
For Each item In q Line 35:
HttpContext.Current.Response.Write(item.Region & vbCrLf) Line 36:
Next Line 37:

Source File: E:\Projects\businesstrader\App_Code\Handlers\RegionsAutoComplete.vb Line: 35

Stack Trace:

[MissingMemberException: Public member 'Region' on type 'String' not found.] Microsoft.VisualBasic.CompilerServices.Container.GetMembers(String& MemberName, Boolean ReportErrors) +509081 Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack) +222 BT.Handlers.RegionsAutoComplete.ProcessRequest(HttpContext context) in E:\Projects\businesstrader\App_Code\Handlers\RegionsAutoComplete.vb:35 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

誰能告訴我什麼,我做錯了什麼?

回答

2

錯誤消息是說存儲在bt_Regions屬性中的對象的類型爲String,因此它們沒有您嘗試訪問的成員Region

我會仔細檢查DAL.bt_Regions是什麼類型 - 它看起來像你假設它返回一些類,但它似乎是返回一個字符串集合(也許只是區域名稱?)。要看到它所包含的內容,你可以修改代碼:

HttpContext.Current.Response.Write(item & vbCrLf) // to print the string 

我也嘗試添加Option Strict On選項(如果可能),這將指示編譯器在編譯時檢查這種錯誤。

+0

啊是的。使用「項目」工作,因爲我只從數據庫中選擇一個記錄(這是一個字符串)。 – 2010-05-17 23:56:49