"Update or Insert" Question

Posts   
 
    
psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 17-Jan-2007 20:04:04   

Let's say I just bought a mailing list of customer leads.

What I want to do is iterate through the the Lead collection and add each one to the Customer collection if it doesn't already exist. If the lead already exists in the Customer collection, I want to update the Customer.

Do I simply do a Find each time to determine if the Lead exists in the Customer collection, and then either add to the collection (if it doesn't exist) or change the Customer entity (if it does)? Is there a better/more efficient way?

Assume for the above that the Lead table has a value that can be matched against the PK of the Customer table.

I must do the above in memory (it's much more complex than my illustration), and also must be as efficient as possible.

Thanks for any insight.

Phil

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 18-Jan-2007 01:52:06   

So you have a collection of customers. And they have a collection of leads? If a customer already has the lead you want to update the lead in the customer's leads collection. If the customer does not have that lead then you want to add it to the leads collection?

In your new list of leads you do not know the PK that you would have in your Lead table?

So you need to go off something like the Name of the lead to see if the customer already has this lead?

If you are using LLBLGen V2.0 then I would get my collection of customers and prefetch the leads. I would then use an EntityView to filter my leads collection on the new leads. Anything new I would then insert into this views lead collection.

Once you've done this for all customers you should be able save recursively to update all customers with their new and updated leads.

If my understanding of the problem is wrong just let us know.

psandler
User
Posts: 540
Joined: 22-Feb-2005
# Posted on: 18-Jan-2007 06:10:00   

Sorry, I don't think I explained it very well.

I already have a list of customers in my database. Now I bought a separate list of customer leads. Some of the leads are already my customers, some of them aren't. So I need to cycle through the list of leads and check if each one is already a customer--if they are, I want to update them. If they're not, I want to add them.

(I don't have my dev machine handy, so this is pretty much pseudo code)


foreach (LeadEntity l in Leads)
{
   bool found = false;
   foreach (CustomerEntity c in Customers)
   {
     if (c.Name == l.Name)
     {
       //update c
       c.PhoneNumber = l.PhoneNumber; //etc.
       found = true;
     }
   }
   if (!found)
   {
     //insert l
     CustomerEntity newCust = new CustomerEntity(l.Name);
     newCust.PhoneNumber = l.PhoneNumber; //etc.   
     Customers.Add(newCust);
   }   
}

Again, this is just an illustration of what I'm trying to accomplish.

My main question is whether the above would be most efficient, or using the Find method, or if there is some other way.

Thanks,

Phil

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Jan-2007 07:41:30   

That's exactly how I would do it.