Skip to content

Instantly share code, notes, and snippets.

View alex-davies's full-sized avatar

Alex Davies alex-davies

View GitHub Profile
@alex-davies
alex-davies / ToBase36String.cs
Created October 26, 2018 08:52
Base36 implementation
public static string ToBase36String(byte[] toConvert)
{
const string alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";
BigInteger dividend = new BigInteger(toConvert);
var builder = new StringBuilder();
while (dividend != 0)
{
dividend = BigInteger.DivRem(dividend, 36, out var remainder);
builder.Insert(0, alphabet[Math.Abs((int)remainder)]);
}
@alex-davies
alex-davies / DictionaryODataFilterLanguage.cs
Last active October 31, 2017 22:16
OData parser language that can work on a `Dictionary<string,object>` (see https://github.com/codecutout/StringToExpression)
using StringToExpression.LanguageDefinitions;
using System;
using System.Collections.Generic;
using System.Text;
using StringToExpression.GrammerDefinitions;
using System.Linq.Expressions;
using System.Linq;
using StringToExpression.Util;
using System.Reflection;
@alex-davies
alex-davies / Type.cs
Created March 15, 2017 22:10
Finds members using expressions
using System;
using System.Linq.Expressions;
using System.Reflection;
namespace StringParser.Util
{
public static class Type<T>
{
/// <summary>
/// Find a MemberInfo based on a linq expression
@alex-davies
alex-davies / ExprssionMapping.cs
Created March 1, 2017 23:16
Given an Expression<Func<A,B>> generates a list of properties that are equivalent.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace ExpressionUtil
{
public class ExpressionMapping
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.OData.Query;
namespace ExpressionUtil
{
@alex-davies
alex-davies / FindVisitor.cs
Created March 1, 2017 23:15
Simple Exprssion visitor to find a particular expression. Works with https://gist.github.com/codecutout/41cceddd4192d8719b6d1b6ca1263483
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace ExpressionUtil
{
public class FindVisitor : ExpressionVisitor
@alex-davies
alex-davies / ExpressionComparer.cs
Created March 1, 2017 23:14 — forked from jnm2/ExpressionComparer.cs
Compares two abstract syntax trees.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
public sealed class ExpressionComparer : IEqualityComparer<Expression>
{
public NameComparison CompareLambdaNames { get; set; }
public NameComparison CompareParameterNames { get; set; }
@alex-davies
alex-davies / ParseODataFilterString.cs
Created March 1, 2017 22:54
Method to parse an odata query string to Expression<Func<T, bool>> using LinqToQuerystring (https://github.com/beyond-code-github/LinqToQuerystring)
public Expression<Func<T, bool>> ParseODataFilterString<T>(string odataQueryString)
{
var query = new List<T>().AsQueryable().LinqToQuerystring("?$filter="+ odataQueryString);
var methodCall = query.Expression as MethodCallExpression;
if (query == null
|| methodCall.Method.Name != "Where"
|| methodCall.Method.DeclaringType != typeof(Queryable))
throw new Exception("cannot process filter query");
@alex-davies
alex-davies / LocalDBHelper.cs
Created February 27, 2017 22:19
Helper class when working with local db to create and drop databases
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
@alex-davies
alex-davies / DynamicProxy.cs
Created August 10, 2016 21:18
Allows the creation of a proxy, where all methods of an interface call a single methods
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;