StrataFrame Forum

Best practice to Enumerate BO to delete current row...

http://forum.strataframe.net/Topic23627.aspx

By Edhy Rijo - 6/19/2009

Hi guys,



I have a BO filtered with records that needs to be deleted from the database, I am trying to use BO.GetEnumerable() to go trough each record and delete it, but of course the once it is deleted, it is removed from the currentview and mess the CurrentRowIndex, here is the code I tried:



Me.BizTransactionItemsStock1.Filter = "Flag_DeletedRecord = True"

For Each StockBO As bizTransactionItemsStock In Me.BizTransactionItemsStock1.GetEnumerable()

StockBO.DeleteCurrentRow(True)

Next





Then I simply decided to use another For..EndFor so the CurrentRowIndex will have no effect like this:



Me.BizTransactionItemsStock1.Filter = "Flag_DeletedRecord = True"

Dim lnReccount As Integer = Me.BizTransactionItemsStock1.Count

If lnReccount > 0 Then

For i As Integer = 1 To lnReccount

Me.BizTransactionItemsStock1.DeleteCurrentRow(True)

Next

End If





This code is working for me, but I want to make sure there is not better way to handle this? of course, after this, a BO.Save() will actually delete the records from the database.



Thanks!



P.S.

To all developers, be aware of the BO.Filter and BO.Sort effects which will immediately remove any record which does to comply with the Filter and for the Sort, it may mess your CurrentRowIndex as well while trying to enumerate the BO. These are very powerful features Smile, but also very tricky BigGrin
By Paul Chase - 6/19/2009

Edhy,

If you loop through backwards and delete then current index won't get out of whack.

 

For i As Integer = MyBO.Count - 1 To 0 Step -1

 If MyBO.somefield = Something Then

Delete

Endif

Next

By Edhy Rijo - 6/19/2009

Hi Paul,



Thanks, I did not even consider that option and sure it does make sense. I will test it.
By Edhy Rijo - 10/12/2009

Paul Chase (06/19/2009)
Edhy, if you loop through backwards and delete then current index won't get out of whack.




Hi Paul,



Just to let you know that today I came across a situation where your suggestion fitted perfectly.



Thanks again for sharing.
By Les Pinter - 10/13/2009

Hi Edhy,

   I used this with a little test database containing a Flag_DeletedRecord column, and it worked fine:

{BO1.Filter = "Flag_DeletedRecord = True";
 if (BO1.Count > 0) { do { BO1.DeleteCurrentRow(); } while (BO1.Count > 0); }
     BO1.Save(); BO1.Filter = "";}

   I hope this relates to what you're doing. Good seeing your name again after all these years...

Les

By Charles R Hankey - 10/13/2009

Les !!



How nice to see you here after all these years. I believe the last time I saw you I was manning the F1 booth near yours at ... Ft. Lauderdale? Palm Springs? I remember Ceil was running around taking pictures. ( man I miss her)



Anyway, welcome to Strataframe. I think you are going to love it and I know your contribution to this community will be very welcome. If you've not yet had the opportunity to meet the Taylor family I know you will enjoy each other.



There is a lot of real-world pragmatism and appreciation of "Microsoft realities" built into this framework and I've found it invaluable in getting very productive in .NET.



Hope we see a lot of you here.



( for the benefit of non-Fox lurkers who may not know him - Les is one of the best known authors, teachers and speakers in the Foxpro world as well as being one of the most colorful and interesting characters in anybody's world BigGrin )
By Edhy Rijo - 10/13/2009

Hi Les,



Glad to see you in the StrataFrame community, it will be very positive for all.



About your sample approach, it is interesting, thanks, I will test it with my project.
By Michel Levy - 10/13/2009

Hi Les,

glad to see you here!