Friday, June 16, 2017

Lambda, lambda, lambda...


Lambda expression, it's an anonymous function it can use to create delegates or expression tree, by using lambda conditions, we can write local functions that can be passed as parameters, let's see it in action for deleting a records with condition in Managed Dbf and it's equivalent Xbase/VFP/Clipper code.

// TASK : Delete all customers from USA country and state of New York or California.


|------------------------------------------------------------------------------------------------------------------------------------------------------


//--> SOLUTION 1:  (SLOW) : Single line of code
using  Where/For condition
//

var
CUSTOMER = new STDB.Table();
CUSTOMER.File.
Use( @"\\MyServer\Fileman\Customer.Dbf", TableShare.Shared, TableAccess.ReadWrite, TableMaxSize.MaxOf16G );
//

CUSTOMER.Remover.
DeleteAll (  true,   rec => cust.C("COUNTRY") == "USA"  &&  (  rec.C("STATE") == "NY" || rec.C("STATE") == "CA");


*--> X
Base / VFP / Clipper  equivalent code
*
USE \\MyServer\Fileman\CUSTOMER.Dbf  SHARED ALIAS cust
GO TOP
DELETE ALL FOR cust.COUNTRY="USA" AND (  cust.STATE="NY" OR cust.STATE="CA" )


|------------------------------------------------------------------------------------------------------------------------------------------------------



//--> SOLUTION 2:  (  FAST ) Using index order by Country. and using For and  While condition overload.
//

var CUSTOMER = new STDB.Table();
CUSTOMER.File.
Use( @"\\MyServer\Fileman\Customer.Dbf", TableShare.Shared, TableAccess.ReadWrite, TableMaxSize.MaxOf16G );//
CUSTOMER.Indexer.SetIndexOrder("COUNTRY");
CUSTOMER.Searcher.
Find("USA");
//


if
(  CUSTOMER.Searcher.Found()  )
{   
      CUSTOMER.Remover.
DeleteAll (  false,
                                        
//For
                                             rec => ( rec.C("STATE") == "NY" || rec.C("STATE")=="CA") ,                                                              
                                                           
//While
                                                                       rec => ( rec.C("COUNTRY") == "USA" )   );
}



*-->  XBase / VFP / Clipper equivalent code
*
USE \\MyServer\Fileman\CUSTOMER.Dbf  SHARED ALIAS cust
SET ORDER TO COUNTRY
SEEK "USA"
IF FOUND()
     DELETE ALL FOR  ( cust.STATE="NY" OR cust.STATE="CA" ) WHILE cust.COUNTRY="USA"
ENDIF


|------------------------------------------------------------------------------------------------------------------------------------------------------

 
//--> SOLUTION 3: ( FASTEST )  Using index order by Country. and State. and While condition only
//

var CUSTOMER = new STDB.Table();
CUSTOMER.File.Use( @"\\MyServer\Fileman\Customer.Dbf", TableShare.Shared, TableAccess.ReadWrite, TableMaxSize.MaxOf16G );
CUSTOMER.Indexer.
SetIndexOrder("COUNTRYSTATE");
//

CUSTOMER.Searcher.Find("USANY");
if
( CUSTOMER.Searcher.Found() )
{                                                                           
//For    //While
     CUSTOMER.Remover.DeleteAll ( falsenull ,   rec => ( rec.C("COUNTRY") == "USA"  &&  rec.C("STATE") == "NY" )   );
}

//


USTOMER.Searcher.
Find("USACA");if (  CUSTOMER.Searcher.Found()  )
{                                                                            
//For    //While
      CUSTOMER.Remover.DeleteAll ( falsenull ,   rec => ( rec.C("COUNTRY") == "USA" &&  rec.C("STATE") == "CA");
}

 
*--> xBase / VFP / Clipper code*
*
USE \\MyServer\Fileman\CUSTOMER.Dbf  SHARED ALIAS cust
SET ORDER TO CNTRYSTAT
*
SEEK "USANY"
IF FOUND()
    DELETE ALL WHILE cust.COUNTRY="USA" AND cust.STATE="NY"
ENDIF
*
SEEK "USACA"
IF FOUND()
    DELETE ALL WHILE cust.COUNTRY="USA" AND cust.STATE="CA"
ENDIF