Skip to content

Instantly share code, notes, and snippets.

@kpespisa
Last active June 2, 2017 07:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kpespisa/361db0638d83afad891b to your computer and use it in GitHub Desktop.
Save kpespisa/361db0638d83afad891b to your computer and use it in GitHub Desktop.
Save and Delete Extensions to SQLite-NET
public static class SQLiteExtensions
{
/// <summary>
/// Save the specified entity by calling insert or update, if the entity already exists.
/// </summary>
/// <param name="pk">The primary key of the entity</param>
/// <param name="obj">The instance of the entity</param>
/// <typeparam name="T">The entity type.</typeparam>
public static int Save<T>(this SQLiteConnection db, object pk, object obj) where T : class, new()
{
if (pk == null || db.Find<T>(pk) == null)
{
return db.Insert(obj);
}
return db.Update(obj);
}
/// <summary>
/// Delete entities based on a predicate function
/// </summary>
/// <param name="predicate">The predicate specifying which entities to delete</param>
/// <typeparam name="T">The entity type.</typeparam>
public static void Delete<T>(this SQLiteConnection db, Expression<Func<T, bool>> predicate) where T : class, new()
{
var records = db.Table<T>().Where(predicate).ToList();
foreach (var record in records)
{
db.Delete(record);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment