New Posts New Posts RSS Feed: Async anonymous LINQ query
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Async anonymous LINQ query

 Post Reply Post Reply
Author
BringerOD View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 27-Aug-2010
Location: USA
Posts: 35
Post Options Post Options   Quote BringerOD Quote  Post ReplyReply Direct Link To This Post Topic: Async anonymous LINQ query
    Posted: 03-Sep-2010 at 7:32am

What is the best practice for getting results of an async anonymous LINQ query?

Just for the record its cool that you can even get this far. Becuase that means DevForce is sending that query to the server and atleast getting back results, where RIA cannot do this.
 
This code works below, just want to make sure this is the best practice.
 
Bryan
 
 
-----------------------------------------------------------------------------
 

var query1 = PrimeEntities.LeadListings

.Where(l => l.GroupId == LoggedIntUser.GroupId)

.GroupBy(l => l.StatusName).Select(g => new

{

g.FirstOrDefault().DisplayOrder,

StatusId = g.FirstOrDefault().StatusId.Value,

StatusName = g.Key,

TotalLeads = g.Count(),

g.FirstOrDefault().FontColor,

g.FirstOrDefault().BackgroundColor

});

((EntityQuery) query1).ExecuteAsync((op) =>

{

StatusGroupDtos.Clear();

foreach (var result in op.Results)

{

Type type = result.GetType();

var newItem = new LeadStatusGroupDto();

newItem.StatusId = (Guid) type.GetProperty("StatusId").GetValue(result, null);

newItem.StatusName = (string) type.GetProperty("StatusName").GetValue(result, null);

newItem.TotalLeads = (int) type.GetProperty("TotalLeads").GetValue(result, null);

newItem.FontColor = (string) type.GetProperty("FontColor").GetValue(result, null);

newItem.BackgroundColor = (string) type.GetProperty("BackgroundColor").GetValue(result, null);

newItem.DisplayOrder = (int?) type.GetProperty("DisplayOrder").GetValue(result, null);

StatusGroupDtos.Add(newItem);

}

});

Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 03-Sep-2010 at 12:54pm
Nice.  Good to see that you're using some of the advanced DevForce features.
 
The op.Results collection is actually strongly typed, so you can directly reference the properties.  For example:
 

  var query = manager.Customers.Select(

    c => new { CompanyName = c.CompanyName, Customer = c, Orders = c.Orders }
  );

 

  query.ExecuteAsync(op => {

    foreach (var anonItem in op.Results) { // op.Results is IEnumerable<a'> strongly typed anon collection

      var companyName = anonItem.CompanyName;

      var customer = anonItem.Customer;

      var orders = anonItem.Orders;

    }

  });

 

 



Edited by ting - 08-Sep-2010 at 6:14pm
Back to Top
BringerOD View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 27-Aug-2010
Location: USA
Posts: 35
Post Options Post Options   Quote BringerOD Quote  Post ReplyReply Direct Link To This Post Posted: 06-Sep-2010 at 3:25pm

Ok, I still might be doing something wrong.

I think the type information is getting lost somewhere.  I do not get back strongly typed anon collection back.  I looked for it when I first tried this. 

 

 

This might have to do with the fact that I have to cast the first group by query back to "EntityQuery"

Back to Top
chuckc View Drop Down
Groupie
Groupie


Joined: 27-Feb-2010
Posts: 54
Post Options Post Options   Quote chuckc Quote  Post ReplyReply Direct Link To This Post Posted: 08-Sep-2010 at 12:10pm
I am seeing the same behavior, that being that I have to cast my var query to an EntityQuery in order to call ExecuteAsync.  And the collection returned in op.Results is not strongly typed.  

Is there some other syntax required?  

Or do you need to use the explicit event handler approach?  Would *that* handle the anonymous type?

Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 08-Sep-2010 at 6:25pm
It looks like the GroupBy clause is breaking the inference and also forcing the cast to EntityQuery.  I'll check with an architect tomorrow and see if there is a solution.  Thanks for pointing this out!
Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 09-Sep-2010 at 3:05pm
Ok, after performing the GroupBy you need to cast to an EntityQuery<IGrouping<string, EntityType>> to restore the type information.  Here's an example:
 

  // Perform the cast below to restore type information after the GroupBy

  var query1 = (EntityQuery<IGrouping<string, Customer>>)manager.Customers.GroupBy(c => c.Country);

  var query2 = query1.Select(g => new { Country = g.Key, Count = g.Count() });

 

  query2.ExecuteAsync(op => {

    foreach (var anonItem in op.Results) {

      var country = anonItem.Country;

      var count = anonItem.Count;

    }

  });

 

We'll look into the extension methods and see if we can do this automatically for you.

 

Back to Top
chuckc View Drop Down
Groupie
Groupie


Joined: 27-Feb-2010
Posts: 54
Post Options Post Options   Quote chuckc Quote  Post ReplyReply Direct Link To This Post Posted: 10-Sep-2010 at 8:58am

Here's another related example of where the type information isn't available in the async result. This scenario doesn't involve a GroupBy:

 

            var q0 = Repository.EntityManager.Certificates.Select(c =>

                        new { PersonLastName = c.Provider.Person.LastName,

                              PersonFirstName = c.Provider.Person.FirstName });

 

            foreach (var i in q0)

            {

                string t = i.PersonLastName; // OK - is strongly typed

            }

 

            ((EntityQuery)q0).ExecuteAsync(op =>

            {

                foreach (var i in op.Results)

                {

                    string t = i.PersonLastName; // FAIL - not strongly typed

                }

            });

 

 

Back to Top
ting View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 27-Mar-2009
Location: San Francisco
Posts: 427
Post Options Post Options   Quote ting Quote  Post ReplyReply Direct Link To This Post Posted: 16-Sep-2010 at 6:35pm
Hi chuckc,
 
I tried an equivalent query and did not need the cast to the EntityQuery and was able to get the strong typing in the results.
 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down