Skip to content

Instantly share code, notes, and snippets.

@mahmoud-samy
Created September 26, 2016 19:36
Show Gist options
  • Save mahmoud-samy/1f07169ade054ed3f1f55e7fc2f2b540 to your computer and use it in GitHub Desktop.
Save mahmoud-samy/1f07169ade054ed3f1f55e7fc2f2b540 to your computer and use it in GitHub Desktop.
using System;
using LiteDB;
namespace LiteDB.Extensions {
public static class LiteCollectionX {
/// <summary>
/// Upserts the specified value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="collection">The collection within which to upsert the <paramref name="value"/>.</param>
/// <param name="value">The value to upsert.</param>
public static void Upsert<T>(this LiteCollection<T> collection, T value) where T : new() {
if (!collection.Update(value)) {
collection.Insert(value);
}
}
public static void Upsert<T>(this LiteCollection<T> collection, T value, Func<T, BsonValue> keySelector) where T : new() {
if (!collection.Update(keySelector(value), value)) {
collection.Insert(value);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment