Skip to content

Instantly share code, notes, and snippets.

View axelheer's full-sized avatar

Axel Heer axelheer

View GitHub Profile
@axelheer
axelheer / MedianExtensions.cs
Created November 27, 2014 06:55
Median extensions for LINQ
public static class MedianExtensions
{
public static double Median(this IEnumerable<int> source)
{
if (source == null)
throw new ArgumentNullException("source");
var data = source.OrderBy(n => n).ToArray();
if (data.Length == 0)
throw new InvalidOperationException();
if (data.Length % 2 == 0)
@axelheer
axelheer / FakeDbSet.cs
Created February 27, 2015 18:54
Fake implementation of Entity Framework's DbSet for fast unit testing
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.SqlServer;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
@axelheer
axelheer / BigIntegerPerf.cs
Last active August 29, 2015 14:19
Basic benchmark for BigInteger
using System.Diagnostics;
namespace System.Numerics.Benchmark
{
public class Program
{
private static readonly Random s_random = new Random(1138);
private static int s_bits = 4096;
private static int s_vals = 100;
@axelheer
axelheer / EnumerableExtensions.cs
Last active November 5, 2019 09:14
Enumerable LINQ extensions
public static class EnumerableExtensions
{
public static IEnumerable<T> Flatten<T>(this T value, Func<T, T> inner, T tail = default(T))
{
var comparer = EqualityComparer<T>.Default;
while (!comparer.Equals(value, tail))
{
yield return value;
value = inner(value);
}
@axelheer
axelheer / DbSeeder.cs
Created August 19, 2015 07:24
Implementation of .AddOrUpdate which supports nullables too
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace System.Data.Entity
{
internal static class DbSeeder
{
@axelheer
axelheer / Resolver.cs
Last active April 10, 2024 10:48
Custom *Assembly load context* as *Module assembly initializer* using *Assembly dependency resolver* to load all the things! Solves dependency conflicts of multiple PowerShell modules and honors .deps.json files...
using System;
using System.Linq;
using System.Management.Automation;
using System.Reflection;
using System.Runtime.Loader;
namespace MyModule
{
public class Resolver : AssemblyLoadContext, IModuleAssemblyInitializer, IModuleAssemblyCleanup
{
@axelheer
axelheer / ConvertTo-CentralPackageManagement.ps1
Last active September 4, 2024 08:21
Generates a `Directory.Packages.props` based on all your project files
function ConvertTo-CentralPackageManagement() {
Write-Host 'Searching for package references'
$packages = @{}
$conditionalPackages = @{}
foreach ($csproj in (Get-ChildItem -Include *.csproj, *.props -Recurse)) {
$root = [xml]($csproj | Get-Content -Raw)
foreach ($itemGroup in $root.Project.ItemGroup) {
foreach ($packageReference in $itemGroup.PackageReference) {
if ($packageReference.Include -and $packageReference.Version) {