Accessing Data with C#

How to Delete Data

There are two overloaded Delete methods to permanently delete data in the Storage. Before deleting existing data items, you need to get their instances (for example, by querying the data store).

Deleting a Single Data Item

To delete a data item from the Data store:

  1. Connect to the data system.
  2. Get an existing item of the data type you need by querying the data store.
  3. Delete the item.
using (DataConnection connection = new DataConnection())
{
   Demo.Users myUser = 
      (from d in connection.Get<Demo.Users>()
       where d.Name == "John Doe"
       select d).First();
   connection.Delete<Demo.Users>(myUser);
}
 

Listing 16: Deleting a single data item

Deleting Multiple Data Items

To delete multiple data items from the Data store:

  1. Connect to the data system.
  2. Get an enumerable list of existing items of the data type you need by querying the data store.
  3. Delete the items in the list.
using (DataConnection connection = new DataConnection())
{
   var myUsers = connection.Get<Demo.Users>().Where (d => d.Number > 15).ToList();
   connection.Delete<Demo.Users>(myUsers);
}

Listing 17: Deleting multiple data items