|
Hi Iosu -
Sorry about the delay and imprecision in our replies. VB is not our first language and we struggle a bit. VB 10 in .NET 4 is new to us. We like the improvements ... but it will take some getting used to.
In VB 9, one could not write "statement lambdas". Now you can. I don't think the conversion tools have caught up.
So I'm going to show you a few ways you could write the query you have in mind.
Please note that the "ForEach" is a DevForce-provided extension method on IEnumerable; it's located in IdeaBlade.Core.EnumerableFns. You have to add the Import.
In all of the following examples, I assume
Imports DomainModel Imports System.Collections.Generic Imports IdeaBlade.EntityModel Imports IdeaBlade.Core
...
Dim employees As New List(Of Employee) Dim query = mgr.Employees ' mgr is an EntityManager instance
Here is the one statement that is closest to the C# expression you mentioned:
query.ExecuteAsync(Sub(op) op.Results.ForEach(Sub(r) employees.Add(r)))
You almost had it! You would have figured this out had we written the C# as
query.ExecuteAsync(op => op.Results.ForEach((r)=> employees.Add(r)))
We took advantage of the C# compiler's ability to translate the method group, "Employees.Add", into the equivalent of "Sub(r) employees.Add(r))". That's what let us write:
query.ExecuteAsync(op => op.Results.ForEach(employees.Add))
VB has some other nice tricks but it doesn't have this one :-)
--
If you don't like our ForEach extension method, you could write the query as:
query.ExecuteAsync( Sub(op As EntityQueryOperation(Of Employee)) For Each emp In op.Results employees.Add(emp) Next End Sub)
--
DevForce also offers an alternative syntax that is more "event oriented."
The ExecuteAsync returns an EntityQueryOperation(Of T) object that has a "Completed" event. You can handle that instead of relying on the callback. The "Completed" handler receives an "EntityQueriedEventArgs(Of T)".
Dim qop = query.ExecuteAsync()
AddHandler _ qop.Completed, Sub(sender, args) args.Results.ForEach(Sub(r) employees.Add(r)) End Sub
--
Once again, should you prefer to do write the "For Each" yourself, you could say:
Dim qop = query.ExecuteAsync()
AddHandler _ qop.Completed, Sub(sender, args) For Each emp In args.Results employees.Add(emp) Next End Sub
--
Important note: we do not show exception handling in these examples. Error information is available both from the Operation object (which is passed into the callback) and in the EntityQueriedEventArgs. Production code would check for errors.
Hope this helps.
|