Print Page | Close Window

EntityQueryBuilder.BuildQuery from array of EntityKeys only creates from first EntityKey EntityType

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2408
Printed Date: 15-Apr-2025 at 1:11pm


Topic: EntityQueryBuilder.BuildQuery from array of EntityKeys only creates from first EntityKey EntityType
Posted By: pk55
Subject: EntityQueryBuilder.BuildQuery from array of EntityKeys only creates from first EntityKey EntityType
Date Posted: 30-Dec-2010 at 1:11pm
I was trying to use the EntityQueryBuilder.BuildQuery method to load a bunch of different entities by passing an array of EntityKey.  Each key in the array is a valid entitytype/key values[] combination but the resulting query only uses the EntityType of the first EntityKey and uses the values from each entity key as if they belonged to the first entity type.
 
Is this expected behavior?  Does this method only work for a single entity type?



Replies:
Posted By: pk55
Date Posted: 30-Dec-2010 at 3:37pm

This behavior is really misleading when no exception is thrown but the entities are loaded that weren't asked for.

In my case, the three entities I have EntityKeys for (one key per entity in this case) happened to use a single int key so the query could be built (here's what the keys contained):
EntityKeys[0].EntityType = User; EntityKeys[0].Values[0] = 1;
EntityKeys[1].EntityType = Status; EntityKeys[1].Values[0] = 3;
EntityKeys[2].EntityType = Group; EntityKeys[2].Values[0] = 4;
The query built was basically:
var query = em.User.Where (x => (((x.UserID == 1) OrElse (x.UserID == 3)) OrElse (x.UserID ==4)));
when it should have created three queries:
var query1 = em.User.Where (x => x.UserID == 1);
var query2 = em.Status.Where (x => x.StatusID ==3);
var query3 = em.Group.Where (x => x.GroupID ==4);
 
I assume there would be an exception thrown if I passed multiple entity keys for different entities with different structures or different number of keys.  Either DevForce should throw an error if it gets mixed EntityTypes or it should only build the where clause based on keys matching the first entity type OR (and this would be my choice) it should do what I had to do:
 
I'm splitting up the EntityKeys into type-specific arrays of EntityKeys in a CoRoutine Parallel Async task and then building and executing the query for each specific type for all matching keys of that type.
 
I assume this is also an issue if you use an EntityKeyList.ToQuery method.  Haven't tried that but it also looks like it expects a single entity type.
 


Posted By: sbelini
Date Posted: 30-Dec-2010 at 3:37pm
Hi pk55,
 
You can only build an EntityQuery<T>. Because of that, you shouldn't have an array with keys from multiple entities.
 
While the above described is the expected behavior (T will be the first entity type found in the array), I think it's not quite appropriate. I'm adding a feature request to check for entity type consistency in the EntityKey array.
 
Silvio.


Posted By: pk55
Date Posted: 30-Dec-2010 at 4:13pm
I suppose that's your only choice since it's just building a query; not executing it at the same time.  I'll just use what I'm doing now.  Thanks.



Print Page | Close Window