'Variable' LINQ Structure

jsurpless

Well-known member
Joined
Jul 29, 2008
Messages
144
Programming Experience
Beginner
Hi all

I've got a LINQ query where I've got an "optional" field that I may not want to query for... the reason being that I may not know the name of that field

I tried doing the following

VB.NET:
If (SomeCondition) then

Dim LINQ_QueryName = From Record in MyTable.AsEnumerable _ ETC

else

Dim LINQ_QueryName = From Record in MyTable.AsEnumerable _ ETC 2

End if

Problem with this is that VSE is telling me that any reference to LINQ_QueryName downstream is invalid because that object is not declared... I can understand how this would be if I only defined it in one area of the IF but I've got it in both?

How would be the way to handle this? Can I append fields to select?

Many thanks!

-Justin
 
Simply put, by declaring the query in each of the branches of your IF conditional you have not expanded is scope but only declared it in two places. To be able to set the value of the query in either branch of the IF and then use it later you need to expand the scope of the variable.

This is done easily by declaring the variable at higher scope such as:

VB.NET:
        Dim myStringOfLetter As String = "ABCDEFG"
        Dim myQuery As System.Collections.Generic.IEnumerable(Of Char)

        If Now.Hour < 12 Then
            myQuery = From myCharacters In myStringOfLetter...
        Else
            myQuery = From myCharacters In myStringOfLetter...
        End If

        Console.WriteLine(myQuery.Count)

Notice that I declared myQuery as a System.Collections.Generic.IEnumerable(Of Char) because that is what I was working with. You can find out what type of IEnumerable your application is inferring for your variable LINQ_QueryName by mousing over it and reading the popup text.

Note that variables, as good practice, should always be declared at the lowest scope possible, so don't go declaring all your variables globally just to ease coding. Ensure that it's a situation like this that warrants a higher scope.
 
Hi

Thanks for the help... just one clarification... I moused over my LINQ query and it's of

System.Data.EnumerableRowCollection(Of <anonymous type>)

How do I declare this type?

Thanks!
 
Hi

I was able to figure out a little of this...

VB.NET:
Dim DatabaseComponent_Table_Query As System.Collections.Generic.IEnumerable(Of DataRow)

                If (c_Database.ZCoordinate_Field <> "") Then

                    DatabaseComponent_Table_Query = From ADG_Instance_Drawing In DatabaseComponent_Table.AsEnumerable() _
                            Select AutoCADReferenceAttribute = ADG_Instance_Drawing.Field(Of Object)(c_Database.ReferenceAttribute_Field), _
                                   Instance_Drawing = ADG_Instance_Drawing.Field(Of Object)(c_Database.InstanceDrawing_Field), _
                                   AutoCAD_Block = ADG_Instance_Drawing.Field(Of Object)(c_Database.AutoCADBlock_Field), _
                                   AutoCAD_Block_XCoord = ADG_Instance_Drawing.Field(Of Object)(c_Database.XCoordinate_Field), _
                                   AutoCAD_Block_YCoord = ADG_Instance_Drawing.Field(Of Object)(c_Database.YCoordinate_Field), _
                                   AutoCAD_Block_ZCoord = ADG_Instance_Drawing.Field(Of Object)(c_Database.ZCoordinate_Field) _
                            Where Instance_Drawing = strAutoCADFile AndAlso AutoCAD_Block IsNot Nothing

                Else

                    DatabaseComponent_Table_Query = From ADG_Instance_Drawing In DatabaseComponent_Table.AsEnumerable() _
                        Select AutoCADReferenceAttribute = ADG_Instance_Drawing.Field(Of Object)(c_Database.ReferenceAttribute_Field), _
                               Instance_Drawing = ADG_Instance_Drawing.Field(Of Object)(c_Database.InstanceDrawing_Field), _
                               AutoCAD_Block = ADG_Instance_Drawing.Field(Of Object)(c_Database.AutoCADBlock_Field), _
                               AutoCAD_Block_XCoord = ADG_Instance_Drawing.Field(Of Object)(c_Database.XCoordinate_Field), _
                               AutoCAD_Block_YCoord = ADG_Instance_Drawing.Field(Of Object)(c_Database.YCoordinate_Field) _
                        Where Instance_Drawing = strAutoCADFile AndAlso AutoCAD_Block IsNot Nothing

                End If

Now, the new issue is that VS can't seem to identify the 'labels' (i.e, AutoCAD_Block_XCoord) that I've define above

VB.NET:
 For Each ADG_Component_Record In DatabaseComponent_Table_Query

                    Dim strAutoCAD_Block_ReferenceAttributeValue As String = ADG_Component_Record.AutoCADReferenceAttribute

Before I add the generic DIM statement, my ADG_Component_Record is of anonymous type... then it becomes of System.Data.DataRow, telling me that the labels aren't part of that type...

How can I deal with this?

Many thanks!

-Justin
 
Back
Top