none
In VB, once I have declared an entity with a two inner joins, how to reference the selected elements. RRS feed

  • Question

  •  

    I am new to entity framework and LINQ. In the code below, I am trying to join 3 tables and then write some values out. None of of the parameters in the 'Debug.WriteLine() calls will compile. How do I refer to u.ID, u.Email in code?

          Using context As New crosurveyEntities

                Dim uas = (From u In context.Users Join s In context.Surveys On u.ID Equals s.UserID _
                           Join t In context.SurveyTemplates On s.SurveyTemplateID Equals t.ID _
                           Select {u.ID, u.Email, u.LastName, s.UserID, s.SurveyTemplate, t.Name})

                Debug.WriteLine(u.ID)
                Debug.WriteLine(t.SurveyTemplateID)
                Debug.WriteLine(u.LastName)
                Debug.WriteLine(uas.ID)


            End Using

    Sunday, September 7, 2014 6:32 AM

Answers

  • Hello David,

    The result “uas” is a collection of anonymous types which could not be referenced as uas.ID, if you want to access on of its properties, you need to create a type, for example MyEntity with properties as ID, Email, LastName, UserID, SurveyTemplate, Name:

    Partial Public Class MyEntity
    
        Public Property ID As Integer
    
        Public Property Email As String
    
        Public Property LastName As String
    
        Public Property UserID As String
    
        Public Property SurveyTemplate As String
    
        Public Property Name As String
    
    End Class 
    

    And change the select as:

    Select New MyEntity With {.ID=u.ID, .Email =u.Email, .LastName =u.LastName, .UserID =s.UserID, .SurveyTemplate =s.SurveyTemplate, .Name =t.Name})

    Since the result is a collection, we could access each record by using foreach:

    For Each e As MyEntity In result
    
                Debug.WriteLine(e.ID)
    
            Next 
    

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 8, 2014 5:35 AM
    Moderator

All replies

  • You are providing just the name of a variable (u for example -> "From u...") but not the Type. This is just a common syntax error for declaring a variable ("i = 0;" won't compile, but int "i = 0;" will), if this doesn't fix it, then ask ahead again, but please provide the error text too.

    Sunday, September 7, 2014 7:20 AM
  • Also... the other problem (if the Linq does in fact yomile and just the Debug.Write... are not) the Variables u and t and s are just available inside the query, you have to use uas instead, it should (since you joined those tables) provide the same Properties as every of the tables you joined in uas.
    Sunday, September 7, 2014 7:24 AM
  • Thanks for your reply. The errors reported are:  

              Debug.WriteLine(u.ID)    u is not declared. It may be inaccessible due to its protection level

               Debug.WriteLine(t.SurveyTemplateID)   Same as above for t
               Debug.WriteLine(u.LastName)

               Debug.WriteLine(uas.ID)      ID is not a member of 'System.Linq.IQueriable(Of Object)

    I think the syntax you suggest, "int "i=0;" might work under C# but not under VB.

    As I have used them they are anonymous variables since their scope is limited to the "Dim uas = " statement. However the example I followed had them being used after the Dim declaration.

    On your second point, I have tried using uas and also go an error.

    David

    Sunday, September 7, 2014 8:10 AM
  • You still just declare uas... u t and s are not declared in your scope. You try to access them after their scope (wich is for the time of executing the statement) ended.

    Since " Select {u.ID, u.Email, u.LastName, s.UserID, s.SurveyTemplate, t.Name}" end the statement, you would get a List of a dynamic type and therefore need to access uas as uas[index].UserID etc.

    Sunday, September 7, 2014 8:52 AM
  • OK - for reasons I don't understand, when I remove the curly braces, I can now use the expression u.ID without generating a compilation error. The other expressions s.UserID and t.Name still get flagged with an error.

    Sunday, September 7, 2014 11:52 AM
  • Hello David,

    The result “uas” is a collection of anonymous types which could not be referenced as uas.ID, if you want to access on of its properties, you need to create a type, for example MyEntity with properties as ID, Email, LastName, UserID, SurveyTemplate, Name:

    Partial Public Class MyEntity
    
        Public Property ID As Integer
    
        Public Property Email As String
    
        Public Property LastName As String
    
        Public Property UserID As String
    
        Public Property SurveyTemplate As String
    
        Public Property Name As String
    
    End Class 
    

    And change the select as:

    Select New MyEntity With {.ID=u.ID, .Email =u.Email, .LastName =u.LastName, .UserID =s.UserID, .SurveyTemplate =s.SurveyTemplate, .Name =t.Name})

    Since the result is a collection, we could access each record by using foreach:

    For Each e As MyEntity In result
    
                Debug.WriteLine(e.ID)
    
            Next 
    

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 8, 2014 5:35 AM
    Moderator
  • Hi Fred,

    Thanks for the reply. Can you tell me where 'result' gets created?

    Also, the code I have now is:

       Partial Class userAndSurvey

            Public Property ID As Integer

            Public Property Email As String

            Public Property LastName As String

            Public Property UserID As String

            Public Property SurveyTemplate As String

            Public Property SurveyName As String


        End Class

            Dim us As userAndSurvey


            Using Context As New croindexEntities

                Dim uas As System.Linq.IQueryable(Of Object)
                uas = (From u In Context.Users Join s In Context.Surveys On u.ID Equals s.UserID _
                           Join t In Context.SurveyTemplates On s.SurveyTemplateID Equals t.ID _
                           Select {us.ID = u.ID,
                                    us.Email = u.Email,
                                    us.LastName = u.LastName,
                                    us.UserID = s.UserID,
                                    us.SurveyTemplate = s.SurveyTemplateID,
                                    us.SurveyName = t.Name
                                    })

                For Each u As userAndSurvey In uas
                    Debug.WriteLine(u.ID)
                Next

    It does not write anything and  it also complains that us is unused.

    David

    Friday, October 3, 2014 9:03 PM
  • Hi,

    >>Can you tell me where 'result' gets created?

    The ‘result’ equals ‘uas’ in your query.

    >> It does not write anything and it also complains that us is unused.

    Having a try to writie the select as:

    Select New userAndSurvey With {.ID=u.ID, .Email =u.Email, .LastName =u.LastName, .UserID =s.UserID, .SurveyTemplate =s.SurveyTemplate, .Name =t.Name})

    Your current query would return an anonymous type.

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, October 6, 2014 7:22 AM
    Moderator