Place the following code behind the Delete
button, again making sure that you use the correct ContactID. The code simply uses the ID to delete the
desired record by calling the Remove method to delete the appropriate record. And, as you have learned,
SubmitChanges is called to send the statement to the database.
AdventureWorks db = new AdventureWorks("Integrated Security=sspi");
var con = db.Contact.Single(c => c.ContactID == 19980);
db.Contact.Remove(con);
db.SubmitChanges();
textBox1.Text = "Contact deleted.";
To delete multiple records, your query would be built along the same lines as the update example for
multiple records.
Working with Objects
You can manipulate data using objects and object members, such as the Add(T) and Remove(T) methods.
It is not that different from what you have already learned in many of the examples in previous chapters.
The following sections show how to use objects to associate LINQ to SQL generic collections to database
objects for submission to the database for execution.
Insert
In the previous insert example, an empty Contact was created and then the individual column properties
were populated with data.
Pages:
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369