2010-05-17 136 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