By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,359 Members | 1,495 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,359 IT Pros & Developers. It's quick & easy.

How to bulk update/insert using Linq

P: 1
Hi, I have recently developed a C# application using Linq. I am getting from an external database a list of profiles I need to process, some are new and some are already in the database, and need to be updated. What I do today is go over the profile list and check each profile if such exists I update otherwise I insert - this solution is working fine.

I am sure there is a way to use bulk insert/update something like UPDATE ON DUPLICATE, this way I can save time since the files I get are huge and bulk insert/update is known to have better performance. I would like to avoid the iteration I am now using.

insertall doesn't work for already stored rows, I need the combination of both update and insert

Here is my code, Your help is highly appreciated.

Expand|Select|Wrap|Line Numbers
  1. foreach (Profile tmpProfile in profiles)
  2.             {
  3.                 try
  4.                 {                      
  5.                     var matchedProfile = (from c in db.ProfileEntities
  6.                                           where c.ProfileId == tmpProfile.Id
  7.                                           select c).SingleOrDefault();
  9.                     if (matchedProfile == null)
  10.                     {
  11.                         //Insert
  12.                         db.ProfileEntities.InsertOnSubmit(EntityMapper.ToEntity(tmpProfile));
  14.                     }
  15.                     else
  16.                     {
  17.                         //Update
  18.                         EntityMapper.ToEntity(ref matchedProfile, tmpProfile);                           
  20.                     }                                               
  21.                 }
  22.                 catch (System.Data.SqlServerCe.SqlCeException sqlExec)
  23.                 {                       
  25.                 }
  26.                 catch (Exception e)
  27.                 {
  29.                 }
  30.             }
  32.             db.SubmitChanges();
Aug 3 '09 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 569
For as far as I know, LINQ can only be used to select; not to insert and update.
Aug 4 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.