See if specific data exsits

Posts   
 
    
Posts: 22
Joined: 20-Sep-2006
# Posted on: 16-Oct-2006 23:22:42   

I'm trying to create a helper function that will see if a record similar to the one that a user is about to add already exists.

In my model there may be cases where the user is allowed to enter another contact with the same First, Last and Company Name but I want to see if one exists and flag the user first.

So what is a good way to go about this, the way I have it setup looks like this



 EntityField2 _companyName = ContactFields.CompanyName;
            EntityField2 _firstName = ContactFields.FirstName;
            EntityField2 _lastName = ContactFields.LastName;
            PredicateExpression filter = ((_companyName == _testContact.CompanyName.ToString()) & (_firstName == _testContact.FirstName) &
                                            (_lastName == _testContact.LastName));
            //EntityCollection<ContactEntity> _contacts = new EntityCollection<ContactEntity>(new ContactEntityFactory());
            ContactEntity _contacts = new ContactEntity();
            using (DataAccessAdapter _adapter = new DataAccessAdapter())
            {
                _adapter.FetchEntityUsingUniqueConstraint(_contacts, filter);
            }

this works, but I'm not sure that it's ideal?

Anyone else do something similar? Also assuming this way is the right way, how do you test for null, the _adapter fills all the fields with an out of sync exception.

Any thoughts?

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 17-Oct-2006 01:56:09   

Well if you have a uniqueconstraint defined for Company Name, First Name, and Last Name then I'm not sure how you could insert a duplicate. I would essentially do what you are doing though assuming that you could have records with duplicate company name, first name, and last name. I would use adapter.FetchCollection() and pass a filter with the smilarities that you are looking for then display these to the user.

If you want to check for null try something like this

bucket.PredicateExpression.Add((OrderFields.OrderDate==System.DBNull.Value));
Posts: 22
Joined: 20-Sep-2006
# Posted on: 17-Oct-2006 03:12:50   

bclubb wrote:

Well if you have a uniqueconstraint defined for Company Name, First Name, and Last Name then I'm not sure how you could insert a duplicate. I would essentially do what you are doing though assuming that you could have records with duplicate company name, first name, and last name. I would use adapter.FetchCollection() and pass a filter with the smilarities that you are looking for then display these to the user.

If you want to check for null try something like this

bucket.PredicateExpression.Add((OrderFields.OrderDate==System.DBNull.Value));

The constraint isn't there, I'm just not sure how to pass in a predicate expression to see if a record with those attributes (first, last, company) exist.

I would like to do this with a collection (you'll notice i've got one commented out in my original post), however FetchEntityCollection takes a relationobject not a IPredicateExpression. If I can do this on a collection, that's the way I want to do it.

Also when I refered to checking for null, I was refering to the entity object. Since the adapter will fill fields with exceptions I can't say


 if(_myEntity != null)
{}

A collection solves the problem since I can get a count.

TIA, Justin

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 17-Oct-2006 07:42:35   

Please try the following code, and then check for the collection count.


RelationPredicateBucket bucket = new RelationPredicateBucket();
bucket.PredicateExpression.Add(ContactFields.CompanyName == _testContact.CompanyName.ToString());
bucket.PredicateExpression.Add(ContactFields.FirstName == _testContact.FirstName);
bucket.PredicateExpression.Add(ContactFields.LastName == _testContact.LastName);

EntityCollection<ContactEntity> _contacts = new EntityCollection<ContactEntity>(new ContactEntityFactory());
using (DataAccessAdapter _adapter = new DataAccessAdapter())
{
        _adapter.FetchEntityCollection(_contacts, bucket);
}

Posts: 22
Joined: 20-Sep-2006
# Posted on: 17-Oct-2006 14:23:59   

That does the trick, Thank You!

Just a quick side question to this, based on the following should I assume that a RelationPredicate isn't always a relationship?

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 17-Oct-2006 15:25:31   

Hi,

a RelationPredicateBucket is a structure the stores both the relation collection and the predicate expression your query is based on.

In your case, there is no relation needed, only the predicate part of it.

mihies avatar
mihies
User
Posts: 800
Joined: 29-Jan-2006
# Posted on: 18-Oct-2006 10:17:39   

I would rather suggest doing SELECT COUNT() FROM ... WHERE ... (translated to LLBLGenPro syntax of course) instead of populating collection. As populating a collection it is an overkill if you don't need that data.

rtbike
User
Posts: 23
Joined: 23-Jan-2009
# Posted on: 31-Jan-2009 00:23:23   

I see this is a old post but i will try here first as this is relivant to the topic.

Using self service how would you do the select count()?

The documentation is a bit confusing, i am only a few weeks into LLBL Gen Pro but working on a large project using it and I am on the learning curve but also under some time constraints.

In the documentation I tried using the Result set fields this seems to make sence to me but it is not working, I know I am missing something

    ResultsetFields checkForCart = new ResultsetFields(1);
    checkForCart.DefineField(CartFieldIndex.UserId, 0, UserID.ToString());
    checkForCart[0].AggregateFunctionToApply = AggregateFunction.Count;
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 31-Jan-2009 08:55:20   

Hi rtbike. Use DynamicLists for more complex or custom data you want to fetch. For simple counts, do this simple_smile :


OrderCollection orders = new OrderCollection();
int numberOfOrders = orders.GetDbCount();

int numerOfOrdersFromALFKI = orders.GetDbCount(OrderFields.CustomerId == "ALFKI");

int numberOfOrdersFromCustomersInGuatemala = orders.GetDbCount(CustomerFields.City == "GTM", 
    new RelationCollection(OrderEntity.Relations.CustomerEntityUsingCustomerId));

P.S. Next time please open a new thread wink

David Elizondo | LLBLGen Support Team