I'm new to Linq to Objects, and I've just hit the problem of anonymous types and scope.
For example, this works just fine:
Public Sub CreateResults()
results = From e In CustomerList
Join f In OrderList On CustomerList.ID Equals OrderList.ID
Select New With {e.FirstName, e.LastName, f.OrderID}
For Each r in results
Console.Writeline(r.FirstName, r.LastName, r.OrderID)
Next t
End Sub
This however does not:
Public Class Foo
Private _linqResults
Public Sub CreateResults()
_linqResults = From e In CustomerList
Join f In OrderList On CustomerList.ID Equals OrderList.ID
Select New With {e.FirstName, e.LastName, f.OrderID}
End Sub
Public Sub PrintResults()
For Each r in _linqResults
Console.Writeline(r.FirstName, r.LastName, r.OrderID)
Next t
End Sub
End Class
I've been looking through SO and other places trying to find a simple solution to this without much luck. Is there any way to access the fields of an anonymous type outside the scope of the method containing the Linq query? Something like:
Console.Writeline(r("FirstName").ToString, r("LastName").ToString)
would be acceptable, though not ideal.
I really, really don't want to start creating additional classes/structures/DTOs to handle what are supposed to be dynamic queries.