Deleting with joins

Posts   
 
    
siegemos
User
Posts: 47
Joined: 25-Jun-2007
# Posted on: 21-May-2009 18:48:13   

Hi,

My apologies if this has been answered before either on this forum or in the documentation. I have looked but couldn't find anything...

I'm trying to translate the following into LLBLGen.

Please note I'm using .Net 2.0 so no LINQ unfortunately.



DELETE FROM EventDay
    FROM EventDay ed  
    INNER JOIN Event e ON ed.EventId = e.Id
    LEFT JOIN EventBooking b ON b.EventDayId = ed.Id 
    WHERE b.Id IS NULL AND e.Id = @EventId


Basically I'm trying to delete all the event days that haven't been booked yet. Is this possible? If so, whats the best way of doing it? Or shall I just write a SPROC?

Thanks

Chris.

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 21-May-2009 21:20:51   

This is possible using "DeleteEntitiesDirectly" - this accepts a RelationPredicateBucket which will allow you to specify the joins and the filters to be used.

Matt

siegemos
User
Posts: 47
Joined: 25-Jun-2007
# Posted on: 22-May-2009 11:39:47   

Thanks for the replay. I should have said, I'm using self servicing so "DeleteEntitiesDirectly" isn't an option. Your post did point me in the right direction though. The standard DeleteMulti has an overload for a relation collection so I managed to achieve what I wanted with the following:



            EventDayCollection days = new EventDayCollection ();

            IRelationCollection relations = new RelationCollection();
            relations.Add(EventDaEntity.Relations.EventEntityUsingEventId);
            relations.Add(EventDaEntity.Relations.EventBookingEntityUsingEventDayId, JoinHint.Left);

            days.DeleteMulti(EventFields.Id == eventId & EventBookingFields.Id == DBNull.Value, relations);



Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 22-May-2009 16:34:31   

Indeed for SelfServicing, you should use the DeleteMulti overload simple_smile

Frans Bouma | Lead developer LLBLGen Pro