LINQ Updates
The following example updates an Account record, given the Id. Note that updates only work on one record at a time, selected by its Id in the WHERE clause.
using System.Linq;
NetSuiteEntities context = new NetSuiteEntities();
var AccountQuery = from Account in context.Account
where Account.Id == "1"
select Account;
foreach (var result in AccountQuery) {
result.acctName = "Checking";
}
try {
context.SaveChanges();
} catch (Exception e) {
Console.WriteLine(e);
}
You can update an entire set by performing multiple queries. The next example builds on the previous example to update an entire set of records and work around the limitation imposed by LINQ of one record per update. Our initial query retrieves a set of records that match the condition (in this instance, all the records whose acctName is "Checking").
Separate queries are then performed for each 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.
NetSuiteEntities context = new NetSuiteEntities();
//Select everything matching the condition
var AccountQuery = from Account in context.Account
where Account.acctName == "Checking"
select Account;
foreach (var result in AccountQuery) {
//For each record matching the condition, perform a separate
//command to update the attributes you desire to change.
var updateRecord = from Account in context.Account
where Account.Id == result.Id
select Account;
//Since the record was selected using the record's unique primary key, you can derive the updateRecord using the
//FirstOrDefault method.
updateRecord.FirstOrDefault().Description = "test desc";
try {
//Commit the changes to the database.
context.SaveChanges();
} catch (Exception e) {
Console.WriteLine(e);
}
}