Using a Join to Update a Column

Posts   
 
    
Animal
User
Posts: 11
Joined: 20-Nov-2008
# Posted on: 29-Dec-2008 05:02:27   

How to use a join in an update, say for example I have an update statement like this:

UPDATE R SET Score=(A.Weight*Q.Weight)/100 FROM Result R, Answer A , Questions Q WHERE ...

I am using LLBLGen Pro 2.6 with Adapter template group. Runtime library version is v2.0.50727 and the database is SQL server 2005 and .Net version is 2.0.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 29-Dec-2008 10:35:41   

You should use UpdateEntitiesDirectly(), and define the joins as EntityRelations in the Relations collection property of the passed in relationPredicateBucket parameter.

Animal
User
Posts: 11
Joined: 20-Nov-2008
# Posted on: 29-Dec-2008 11:05:06   

I think I did not emphasize the actual problem....What I am not able to achieve is Score=(A.WeightQ.Weight)/100* part in the sample update statement in the original post. I think it is something trivial but I am lost confused

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 29-Dec-2008 11:29:15   

Try something like the following (improvised and not tested):

IExpression updateExpression1 = new Expression(AnswerFields.Weight, ExOp.Mul,
QuestionsFields.Weight);
IExpression updateExpression2 = new Expression(updateExpression1, ExOp.Div,
100);

ResultEntity result = new ResultEntity();

result.Fields[(int)ResultFieldIndex.Score].ExpressionToApply = updateExpression2;

DataAccessAdapter adapter = new DataAccessAdapter();
adapter.UpdateEntitiesDirectly(result, null);

Please check Expressions in entity updates

Animal
User
Posts: 11
Joined: 20-Nov-2008
# Posted on: 30-Dec-2008 03:01:28   

Thanks Walaa, it works fine. Before posting I did search in the help but failed to notice it cry