Skip to content

Instantly share code, notes, and snippets.

@Stuyk
Last active April 11, 2019 15:32
Show Gist options
  • Save Stuyk/2ed612997088854125945eb570b47c0d to your computer and use it in GitHub Desktop.
Save Stuyk/2ed612997088854125945eb570b47c0d to your computer and use it in GitHub Desktop.
Get By Collection / Selection Collection
/// <summary>
/// Get an entire collection by Type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<T> GetCollection<T>()
{
try
{
using (var db = new LiteDatabase($"{DatabaseHelper.DatabasePath}/Database.db"))
{
return db.GetCollection<T>().FindAll().ToList();
}
} catch (Exception ex)
{
Console.WriteLine(ex);
return null;
}
}
/// <summary>
/// Select a collection from the database based on very specific info.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="field"></param>
/// <param name="data"></param>
/// <returns></returns>
public static List<T> GetCollectionFromSelection<T>(string field, BsonValue data)
{
try
{
using (var db = new LiteDatabase($"{DatabaseHelper.DatabasePath}/Database.db"))
{
return db.GetCollection<T>().Find(Query.EQ(field, data)).ToList();
}
} catch (Exception ex)
{
Console.WriteLine(ex);
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment