Skip to content

Instantly share code, notes, and snippets.

@Dessix
Created January 2, 2016 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dessix/27a9820394f643df30b3 to your computer and use it in GitHub Desktop.
Save Dessix/27a9820394f643df30b3 to your computer and use it in GitHub Desktop.
Basic Upsert substitute
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