Integration Your Way - (800) 235-7250
RSSBus ADO.NET Provider for SalesForce V2
Questions / Feedback? LINQ Updates

LINQ Updates

The following example updates an Accounts record given an Id. Note that updates only work on one record at a time as, selected by their Id in the "where" clause.

using System.Linq;

//...

SalesForceEntities context = new SalesForceEntities();

var *exLINQTable;Query = from Accounts in context.Accounts
                   where Accounts.Id == "anID"
                   select Accounts;

foreach (var result in AccountsQuery) {
  result.Industry = "Floppy Disks";
}

try {
  context.SaveChanges();
} catch (Exception e) {
  Console.WriteLine(e);
}

The next example updates an entire set of records by building on the previous example and working around the limitations imposed by LINQ that limit you to one record per update. We can update an entire set by performing multiple queries. Our initial query retrieves a set of records match a desired condition (the condition being, in this instance, all the records whose Industry is "testIndustry").

Separate queries are then performed for each record Id, whose descriptions are then changed and saved. Note the performance penalty for this approach: a separate connection must be established for each desired update.

SalesForceEntities context = new SalesForceEntities();

//Select everything matching our desired condition
var AccountsQuery = from Accounts in context.Accounts
                   where Accounts.Industry == "testIndustry"
                   select Accounts;

foreach (var result in AccountsQuery) {
  //For each record matching our condition, perform a separate
  //command to update the attributes we desire to change.
  var updateRecord = from Accounts in context.Accounts
                     where Accounts.Id == result.Id
                     select Accounts;
                     
  //Since we've selected using the record's Id, and all Id's
  //are unique, we can derive the updateRecord using the
  //FirstOrDefault() method.
  updateRecord.FirstOrDefault().Description = "test desc";

  try {
    //Commit our changes to the database.
    context.SaveChanges();
  } catch (Exception e) {
    Console.WriteLine(e);
  }
}


 
Copyright © 2013 RSSBus Inc.
[x] close

Questions / Feedback?


Name:
Email:
Feedback:
Send Feedback