Created
January 2, 2016 00:20
-
-
Save Dessix/27a9820394f643df30b3 to your computer and use it in GitHub Desktop.
Basic Upsert substitute
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
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