That article describes the difference between Immediate and Deferred execution (see below).
It does not address named types; it does not answer the immediate question.
You stated: “A new ParentData1 object is created for each match in query.”
I replied: “I don’t understand where the objects are created.
In the Linq query, what is the object container?”
The article says in part:
Query execution is separate from creating a query. After a query is created, its execution is triggered by a separate mechanism. A query can be executed as soon as it is defined (immediate execution), or the definition can be stored and the query can be executed later (deferred execution).
By default, when you create a query, the query itself does not execute immediately. Instead, the query definition is stored in the variable that is used to reference the query result. When the query result variable is accessed later in code, such as in a For…Next loop, the query is executed. This process is referred to as deferred execution.
Introduction to LINQ in Visual Basic
Another site explains the basic parts of a Linq query.
Writing Your First LINQ Query (Visual Basic)
' Data source.
Dim numbers() As Integer = {0, 1, 2, 3, 4, 5, 6}
' Query creation.
Dim evensQuery = From num In numbers
Where num Mod 2 = 0
Select num
' Query execution.
For Each number In evensQuery
Console.Write(number & " ")
Next
My first issue with the article is “the query can be executed later”.
What does “later” mean? My interpretation is: in another Sub that’s called later.
That’s what my main question relates to: “how can the results be made available to other procedures?”
For this to happen, the Dim must be module-wide, correct?
Module
Dim Query1 = Nothing
Sub MakeQuery()
Query1 = From num In numbers ‘query is modified here
…
Sub UseQuery()
For each p in Query1…
The availability of Query1 “later” is not affected by “anonymous” or “named” type options.
The second issue is “the query itself does not execute immediately”.
This is saying the query itself does not generate objects.
Only when the query is executed (e.g. in a For Each loop) is the query executed.
Contrary to your statement “A new ParentData1 object is created for each match in query.”
I don’t see objects created unless my explicit code does it.
As a novice, I see these steps to create objects from a Linq to SQL query:
Public Class Person1
Property firstName As String
Property lastName As String
End Class
Module Module2
Dim db As New MyFamilyDataContext
Dim person1Col As New Collection
Sub dbGetData3()
'SQL table Person2
Dim Query3 = From p In db.Person2s
Select p.firstName, p.lastName 'anonymous type
Dim cnt = Query3.Count 'Query is declared locally; .Count is available
For Each p In Query3
MakeObject(p.firstName, p.lastName) 'Makes instance of class Person1
Next
End Sub
Sub MakeObject(ByVal firstName As String, ByVal lastName As String)
Dim person1 As New Person1 With {.firstName = firstName, .lastName = lastName}
person1Col.Add(person1)
End Sub
The person1Col collection is available for later queries.
The “extra” code is class Person1, not needed otherwise.
This code seems to work and answers my original question.
If there is an easier or better way to do this with “named types” that I fail to understand, please let me know.