Skip to content

Instantly share code, notes, and snippets.

View DrewQuick's full-sized avatar

Drew Quick DrewQuick

View GitHub Profile
@DrewQuick
DrewQuick / CacheEval.cs
Created July 12, 2021 15:19
Cache Eval
// Implementation
private const int DEFAULT_RETENTION = 60;
public static T Eval<T>(this IMemoryCache cache, string key, Func<T> expression)
=> Eval(cache, key, DEFAULT_RETENTION, expression);
public static T Eval<T>(this IMemoryCache cache, string key, int retentionMinutes, Func<T> expression)
{
T val;
if (!cache.TryGetValue<T>(key, out val))
@DrewQuick
DrewQuick / MergeEntityListToDB.cs
Last active July 26, 2017 14:30
Dynamically inserts/updates a list of entity framework models into the database
public static void MergeEntityListToDB<T>(List<T> list) where T : class
{
if (list.Count == 0)
throw new Exception("No records to update in list.");
var db = new DataBaseEntities(); // Whatever you named your EF DB context in ADO.NET
// Get Property Info of Entity Type
var propertyInfo = db.GetType().GetProperties().Where(x => x.PropertyType == typeof(DbSet<T>)).FirstOrDefault();
if (propertyInfo == null)
@DrewQuick
DrewQuick / ExecuteMethod.cs
Last active July 12, 2021 15:21
Dynamic Invocation
public static void ExecuteMethod(string MethodName, Type ClassType = null)
{
if (string.IsNullOrWhiteSpace(MethodName))
throw new Exception("First argument must contain the target method name.");
if (ClassType == null)
ClassType = new StackFrame().GetMethod().DeclaringType;
var methodInfo = ClassType.GetMethods().Where(x => x.Name.ToLower() == MethodName.ToLower()).FirstOrDefault();
if (methodInfo == null)
@DrewQuick
DrewQuick / DataTableToEntityList.cs
Created July 26, 2017 14:23
Dynamically convert datatable into a list of entities
public static List<T> DataTableToEntityList<T>(DataTable dt)
{
Logging.Info("Converting datatable to entity list");
foreach (DataColumn column in dt.Columns)
{
dt.Columns[column.ColumnName].ColumnName = column.ColumnName.Replace(" ", "").Replace("-", "").ToLower();
}
var list = new List<T>();
@DrewQuick
DrewQuick / ExcelToDataTable.cs
Last active July 26, 2017 14:23
Uses ExcelReader to read a local Excel file into a dataset
public static DataTable ExcelToDataTable(string FileName)
{
Logging.Info("Reading Excel file to datatable: " + FileName);
var ds = new DataSet();
using (var stream = File.Open(FileName, FileMode.Open, FileAccess.Read))
{
using (var reader = ExcelReaderFactory.CreateReader(stream))
{