Best way to check if data exists in the DB

Posts   
 
    
MarkReed
User
Posts: 7
Joined: 01-Mar-2005
# Posted on: 04-Mar-2005 12:30:03   

I am new to LLBLGen pro and want to know the best way to check and see if a specific record (using PK) exists.

My table is called "users" and say i want to check ad see if user with loginID = "bob" is in the users table:

I am using 2 class method and have created the following static function in my entity class:

public static bool Exists(String sLoginID) { UsersCollection users = new UsersCollection(); IPredicateExpression selectFilter = new PredicateExpression(

            PredicateFactory.CompareValue(UsersFieldIndex.LoginID, 
            ComparisonOperator.Equal, sLoginID));

            users.GetMulti(selectFilter);

if (users.Count > 0) //the user is in the DB
{
    return true;
}
else
{
    return false;
}

}

The above works, but i dont want to have to write a function like this for all entity classes. I am sure LLBLGen must already do this without me writing a function, if so how?

thanks in advance

MarkReed
User
Posts: 7
Joined: 01-Mar-2005
# Posted on: 04-Mar-2005 17:02:11   

Im doing this now...much better

UsersEntity user = new UsersEntity(sLoginID);

bool exists = (user.Fields.State == EntityState.Fetched);

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 04-Mar-2005 20:37:40   

Another way is to execute a scalar query on the PK field. A scalar query can be a bit faster than loading a full object into memory, though as it's about a single object, it's not that much of a difference.

Frans Bouma | Lead developer LLBLGen Pro