Skip to content

Instantly share code, notes, and snippets.

@jmcd
jmcd / MoveOperation.cs
Created October 14, 2011 15:16
General purpose move operation on entities with an integer rank
using System;
using System.Collections.Generic;
using System.Linq;
namespace jmcd
{
public class MoveOperation<T>
{
private readonly Func<T, int> _positionOf;
private readonly Action<T, int> _reposition;
@jmcd
jmcd / WtfApp.cs
Created December 2, 2011 14:24
A console app to print the current memory usage of all worker processes of an IIS app-pool.
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.Web.Administration;
namespace WtfApp
{
internal class Program
@jmcd
jmcd / Example AddressFields.cshtml
Created March 20, 2012 16:00
Extension to render partials of nested models with correct element name prefix
@model gts.Web.Models.AddressFields
<ul>
<li>
@Html.HumanLabelFor(x => x.Line1)
@Html.TextBoxFor(x => x.Line1)
@Html.ValidationMessageFor(x => x.Line1)
</li>
<li>
@Html.HumanLabelFor(x => x.Line2)
@jmcd
jmcd / Foo.cs
Created April 19, 2012 09:42
QueryOver select except
var scheduleDateAlias = default(ScheduleDate);
return session.QueryOver(() => scheduleDateAlias)
.WithSubquery.WhereNotExists(
QueryOver.Of<CommunityGrantApplicationChequeRequest>()
.Where(x => x.ScheduleDate.Id == scheduleDateAlias.Id)
.Select(x => x.ScheduleDate)
)
.OrderBy(scheduleDate => scheduleDate.Rank).Asc
.List<ScheduleDate>();
@jmcd
jmcd / ComboMaskFactory.cs
Created May 8, 2012 07:49
Creates permutations of an integer for a fixed number of '1' bits
public class ComboMaskFactory
{
private int _current;
private readonly int _lim;
public ComboMaskFactory(int maxNumberOfBitsInMask, int numberOfOneBits)
{
if (maxNumberOfBitsInMask > 32) throw new Exception("ComboMaskFactory can only be used with maxNumberOfBitsInMask up to 32.");
_current = GetLowestValueUsingSuppliedNumberOfOneBits(numberOfOneBits);
_lim = 1 + _current << maxNumberOfBitsInMask - numberOfOneBits;
@jmcd
jmcd / IntExtensions.cs
Created May 8, 2012 07:56
Ordinal suffix of an integer
public static class IntExtensions
{
public static string GetOrdinalSuffix(this int @this)
{
switch (@this % 100)
{
case 11:
case 12:
case 13:
return "th";
@jmcd
jmcd / Act.cs
Created May 8, 2012 15:17
Making an action expression from a function expresstion
var funcExp = (MyThing x) => x.MyMethod(foo);
var actExp = Expression.Lambda<Action<MyThing>>(funcExp.Body, funcExp.Parameters);
@jmcd
jmcd / gist:2660070
Created May 11, 2012 14:30
Adding paths to view engine without subclassing
var viewEngine = (RazorViewEngine)ViewEngines.Engines.Single(ve => ve is RazorViewEngine);
viewEngine.PartialViewLocationFormats = new List<string>(viewEngine.PartialViewLocationFormats)
{
"~/Views/Shared/Fields/{0}.cshtml",
"~/Views/Shared/ReadOnlyModels/{0}.cshtml",
}.ToArray();
public static class ResolveUrlExtension
{
public static MemberConfigurationExpressionWrapper<TSource> ResolveUrl<TSource>(this IMemberConfigurationExpression<TSource> opt)
{
return new MemberConfigurationExpressionWrapper<TSource>(opt);
}
public class MemberConfigurationExpressionWrapper<TSource>
{
private readonly IMemberConfigurationExpression<TSource> _opt;
@jmcd
jmcd / gist:3187094
Created July 27, 2012 09:30
Delete bin and obj directories
for /d /r . %d in (bin,obj) do @if exist "%d" rd /s/q "%d"