Delete entities based on a generic list parameter

Posts   
 
    
Stephan
User
Posts: 63
Joined: 16-Jan-2007
# Posted on: 05-Sep-2007 23:33:54   

Hi there, I'm not sure if it is possible. But I want to remove some entities at the same time. I have a function:

private static void RemoveAllDiscipline(int workGoalId, IList<int> disciplineIDs) { //remove all discipline using (DataAccessAdapter adapter = new DataAccessAdapter()) { IRelationPredicateBucket filter = new RelationPredicateBucket(); filter.PredicateExpression.Add(IbpWorkPlanFields.IbpworkGoalId == workGoalId); filter.PredicateExpression.Add(IbpWorkPlanFields.IbpdisciplineId == disciplineIDs); adapter.DeleteEntitiesDirectly("IbpWorkPlanEntity", filter); } }

As you can see disciplineIDs is a generic list. But I'm not sure if I can use it in this way. In the documentation it is said that if you want to use a where in (x,y,z) clause you could do it like this, but when I use it like this an exception is thrown.

SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException was unhandled by user code Message="An exception was caught during the execution of an action query: Failed to convert parameter value from a List`1 to a Int32..

If this is not the way, what is? Do I have to iterate through the collection and delete the entity one at a time?

Thx

psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 06-Sep-2007 00:02:47   

I believe your disciplineIDs variable needs to be an array of integers, not an IList.

You could add the line:

int[] disciplineIDsArray =  ((List<int>)disciplineIDs).ToArray();

And then use the new variable in your predicate instead. I'm making some assumptions, and the above code isn't syntax-checked, but I think it should work.

HTH,

Phil

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Sep-2007 05:31:12   

Copied from docs (v2 and v2.5):

// C#
int[] values = new int[3] {1, 2, 5};
bucket.PredicateExpression.Add(new FieldCompareRangePredicate(
    OrderFields.OrderDate, null, values));

// which is equal to:
bucket.PredicateExpression.Add(OrderFields.OrderDate == values);

You can use an ArrayList as well.

David Elizondo | LLBLGen Support Team
Stephan
User
Posts: 63
Joined: 16-Jan-2007
# Posted on: 06-Sep-2007 08:28:12   

Hi there,

thanks for the reply. converting to an array of the int-type worked fine. In the documentation I saw that there are three options: - an Array - an ArrayList - directly in the call to the predicate creation

So I had to cast to an array or arraylist. Now it works fine. I used Phills solution. The syntax was ok Phill. Thanks