Client Interfaces Guide > TIBCO ADO.NET 2020 Data Provider for TDV > Using ADO.NET (LINQ) > LINQ Deletes
 
LINQ Deletes
This section describes several ways to delete TDV objects with LINQ.
Deleting TDV Objects with LINQ to Entities
The following example uses LINQ query syntax to obtain the object to be deleted, then deletes the object from the data model. Note that deletes only work on one record at a time, selected by its Id in the WHERE clause.
 
CompositeEntities context = new CompositeEntities();
var query = from Products in context.Products
where Products.Id == "22"
select Products;
context.Products.Remove(query.FirstOrDefault());
try {
context.SaveChanges();
} catch (Exception e) {
Console.Write(e.Message);
}
Using LINQ Element Operators
The SingleOrDefault and FirstOrDefault element operators provide alternative ways to obtain an object you want to delete. You can also use these element operators to simplify deleting multiple records.
 
Selecting a Single Entity to Delete
Another way to accomplish the same result as the preceding query is to use the SingleOrDefault element operator. For example:
 
CompositeEntities context = new CompositeEntities();
var Products = context.Products.SingleOrDefault(o => o.Id == "150000-933272658");
context.Entry(Products).State = System.Data.Entity.EntityState.Deleted;
context.SaveChanges();
 
Deleting a Set of Entities
The following example deletes a group of records whose ProductName value equals "Konbu". Note that LINQ only performs one delete operation per record at a time as specified by the Id of the record. Because of this, LINQ incurs a performance penalty as it opens a fresh connection with the data source every time it deletes a record.
 
CompositeEntities context = new CompositeEntities();
var query = from Products in context.Products
where Products.ProductName == "Konbu"
select Products;
foreach (var result in query) {
var delete = from Products in context.Products
where Products.Id == result.Id
select Products;
context.Products.Remove(delete.FirstOrDefault());
}
try {
context.SaveChanges();
} catch (Exception e) {
Console.Write(e.Message);
}