PredicateExpression with thousands of "AddWithOR"

Posts   
 
    
Homer00
User
Posts: 10
Joined: 27-Dec-2010
# Posted on: 09-Mar-2011 21:23:41   

Hi,

I have a list of customers id from a .txt file. I split them into an array (about 4500 ids). Now i have to query the database to get customer info using the ids that i have splited.

SD.LLBLGen.Pro.ORMSupportClasses.PredicateExpression PE = new SD.LLBLGen.Pro.ORMSupportClasses.PredicateExpression();

foreach(int id in customers_list) { // this 4500 times PE.AddWithOr(TDB.HelperClasses.Customers.Id == id); }

I suppose that this is brute force and will be a better method but i'm newbie

My problem is that i need a method to filter a query without a memory exception simple_smile

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 10-Mar-2011 05:43:34   

What you are doing now is translated to SQL as:

SELECT * FROM MyTable
WHERE 
  Foo = 1 OR
  Foo = 2 OR
  Foo = 3 OR
  Foo = ... OR
  Foo = 4500

Instead you could use an IN clause like:

SELECT * FROM MyTable
WHERE Foo IN (1,2,3,...,4500)

For that you can use FieldCompareRangePredicate. Now, SQLServer has a parameter limitation. If I'm not wrong the limit is 2100 params. That means you can't do this in one shot. You have to fetch the records in small pieces. For instance:

// this retrieves the first 2000 items from customers_list
List<int> firstBucket = GetBucketOfIds(1, 2000);
IRelationPredicateBucket filter1 = new RelationPredicateBucket();
filter1.Add(MyTableFields.Foo == firstBucket);
EntityCollection<MyTableEntity) firstCollection = new EntityCollection<MyTableEntity>();
adapter.FetchEntityCollection(firstCollection, filter1);

// do the same 2 or 3 times to fetch the other collections

// now you can merge the 2 or 3 sub collections

Other way is that you can insert those Ids into a temp table in your database. Now you can do a FieldCompareSetPredicate to do it in one shot.

David Elizondo | LLBLGen Support Team
Homer00
User
Posts: 10
Joined: 27-Dec-2010
# Posted on: 10-Mar-2011 09:05:04   

Very good answer!

So much thanks. I like your idea:

Other way is that you can insert those Ids into a temp table in your database. Now you can do a FieldCompareSetPredicate to do it in one shot.

Me ha ayudado mucho tu respuesta. Gracias mil!

Bye