New Posts New Posts RSS Feed: ArgumentNullException thrown when using OfType<T> in 5.2.2
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

ArgumentNullException thrown when using OfType<T> in 5.2.2

 Post Reply Post Reply
Author
sebma View Drop Down
Groupie
Groupie
Avatar

Joined: 19-Aug-2008
Location: Singapore
Posts: 66
Post Options Post Options   Quote sebma Quote  Post ReplyReply Direct Link To This Post Topic: ArgumentNullException thrown when using OfType<T> in 5.2.2
    Posted: 17-Sep-2009 at 1:45am
Hi,
 
I upgraded to 5.2.2, and seems like I have to do workarounds of not using OfType<T> when attempting to get an IEntityQuery.
 
// In 5.1.0, we use EntityQuery and works even when there is nothing in database yet
EntityQuery<Organisation> query510 = EntityManager.Parties.OfType<Organisation>().ToQuery<Organisation>();
 
// In 5.2.2, we use IEntityQuery and System.ArgumentNullException will be thrown if there is nothing in database yet
EntityQuery<Organisation> query522 = EntityManager.Parties.OfType<Organisation>().ToQuery<Organisation>();
 
// Hence in 5.2.2, we have to do something like this
if (EntityManager.Parties.Where(o => o is Organisation).FirstOrDefault() != null)
{
    IEntityQuery<Organisation> query522 = EntityManager.Parties.Where(o => o is Organisation).ToList().Cast<Organisation>().ToQuery<Organisation>();
    ...
}
 
Is this as design?
 
Thanks.
Sebastian
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 17-Sep-2009 at 6:08pm
The ArgumentNullException certainly wasn't intentional, but this has never worked the way you think it does.

EntityQuery<Organisation> query510 = EntityManager.Parties.OfType<Organisation>().ToQuery<Organisation>();
 
The ToQuery<Organisation>() clause actually causes two things to occur -
 
1) First and most importantly, and probably not your intention, is it causes the query, EntityManager.Parties.OfType<Organisation>(), to be enumerated.   This results in the query being sent to the database at this time.  You can see why the query is enumerated in looking at the method signature:

public static EntityQuery<T> ToQuery<T>(this IEnumerable<T> entities) where T : Entity
 
2) Once the entities are returned, a new (possibly quite large) query is built based on the keys returned.  So you end up with a query which looks something like this - ... Where ... and (Id=1 or Id=2 or Id=3 ...).  You can see the query built by doing a query510.ToString() call.   You can also use a profiler to see what's sent to the database when query510.ToList() is executed.  It's likely not pretty.
 
So, I'm assuming what you really want is to build a query which filters for a sub-type, and control how the query is executed either through enumeration or explicit execution.  The easiest way to do this is to leave out the ToQuery() -

EntityQuery<Organisation> query510 = EntityManager.Parties.OfType<Organisation>();
var results = query510.ToList();
For the record, there are other ways to build up a query for a sub-type, here are a few -

var query2 = EntityManager.Parties.Where(o=> o is Organisation);
var query3 = EntityManager.GetQuery<Organisation>();
 
 
 
 
 
 
 
Back to Top
sebma View Drop Down
Groupie
Groupie
Avatar

Joined: 19-Aug-2008
Location: Singapore
Posts: 66
Post Options Post Options   Quote sebma Quote  Post ReplyReply Direct Link To This Post Posted: 17-Sep-2009 at 8:42pm
Yes, seems like you read my mind. Certainly don't like the idea of client going back and forth to DB with chunks of SQL statements. Often a mistake with LINQ's ease of use.

I'll use GetQuery<T>().

Many thanks again!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down