Last active
June 2, 2017 07:04
-
-
Save kpespisa/361db0638d83afad891b to your computer and use it in GitHub Desktop.
Save and Delete Extensions to SQLite-NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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