EXISTS

Posts   
 
    
changomarcelo avatar
Posts: 62
Joined: 15-Feb-2007
# Posted on: 27-Oct-2007 20:36:53   

Hi! I have a table called Accounts, and there is a Name field (Accounts.Name).

How do I know if a specific Name exists in the Accounts table?

So far, I've been using the following code, but I want to know if there's a recommended way:


        public static bool AccountExists(string name)
        {
            AccountCollection accounts = new AccountCollection();
            IPredicateExpression filter = new PredicateExpression(AccountFields.Name == name);
            return (accounts.GetDbCount(filter) == 1);
        }
Posts: 254
Joined: 16-Nov-2006
# Posted on: 27-Oct-2007 22:25:32   

Well there are a few approaches however there is nothing inherently wrong with what you've written.

It's mostly desirable to try and place the code which creates the predicates / relations e.t.c. inside either the entity classes themselve or in other manager / controller type classes. You will soon find yourself wanting to put checks such as AccountExists in other places too.

However if the predicates get relatively complex and are very specifc to the client scenario then they are probably best kept in the client code.

Reviewing your code you obviously only expect there to be one account with the matching name i.e. == 1 and not >= 1. In this case you could create a unique constraint on the Name field, in which case LLBLGEN would generate an overloaded constructor which would fetch an Account entity based on this value. You could then simply create the Account entity and check for the IsNew property which would determine whether it's existing or doesn't exist.

ggpnet
User
Posts: 21
Joined: 07-Apr-2005
# Posted on: 28-Oct-2007 22:29:13   

I think this is the easiest way

bool returnValue = Enum.IsDefined(typeof(AccountFieldIndex), "Name");
Posts: 254
Joined: 16-Nov-2006
# Posted on: 28-Oct-2007 22:41:48   

bool returnValue = Enum.IsDefined(typeof(AccountFieldIndex), "Name");

This code does something completely different to your original code snippetconfused

This code would simply return true of a Name field existed in the Account entity.

ggpnet
User
Posts: 21
Joined: 07-Apr-2005
# Posted on: 29-Oct-2007 00:54:27   

changomarcelo wrote:


        public static bool AccountExists(string name)
        {
            AccountCollection accounts = new AccountCollection();
            IPredicateExpression filter = new PredicateExpression(AccountFields.Name == name);
            return (accounts.GetDbCount(filter) == 1);
        }

Hi Matt,

I don't understand what you mean, the original code snippet is not mine, it's from changomarcelo.

public static bool AccountExists(string name)

This method just check that the Name Field or any other Field exists in SomeEntity and return a bool value.

That's what this code does

bool returnValue = Enum.IsDefined(typeof(AccountFieldIndex), "Name");

and isn't what changomarcelo wants ?

Regards Gianfranco

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 29-Oct-2007 01:37:06   

Isn't clear whether changomarcelo wants to know if exists some field named "Name" or if exists some record that contains in the field Name a value passed by a variable (name). Seeing the original code seems to me that his code is completely valid simple_smile

David Elizondo | LLBLGen Support Team
changomarcelo avatar
Posts: 62
Joined: 15-Feb-2007
# Posted on: 29-Oct-2007 02:42:37   

Hi, thank you all. What I wanted to do exactly is to know if a specified account name exists in the Name field of my Accounts table. For example, I want to know if the account named 'xyz' exists.

As MattAdamson noticed, I don't want the accounts names to repeat. I followed his advice and added a unique constraint for the Name column.

However it wasn't clear what he meant regarding where to place that code. That code is currently in an AccountsController class in my BL project, following a similar approach to the HnD example provided by the makers of LLBLGen Pro.

Thanks!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 29-Oct-2007 03:11:37   

changomarcelo wrote:

As MattAdamson noticed, I don't want the accounts names to repeat. I followed his advice and added a unique constraint for the Name column.

However it wasn't clear what he meant regarding where to place that code. That code is currently in an AccountsController class in my BL project, following a similar approach to the HnD example provided by the makers of LLBLGen Pro. Thanks!

That's ok simple_smile ... you are going on a suitable path wink

David Elizondo | LLBLGen Support Team