Skip to content

Instantly share code, notes, and snippets.

public static class ArrayUtils
{
public static void Push<T>(this T[] arr, T newItem)
{
arr = (new T[] { newItem }).Concat(arr).ToArray();
}
public static T Pop<T>(this T[] arr, T newItem)
{
T retval = default(T);
public static IEnumerable<T> Distinct(this IEnumerable<T> set, Func<T,U> propertySelector)
{
HashSet<U> distinct = new HashSet<U>();
foreach(var a in set)
{
var key = propertySelector.Invoke(a);
if(!distinct.Contains(key))
{
distinct.Add(key);
yield return a;
using System;
using System.Collections.Generic;
public class MyClass
{
public static void RunSnippet()
{
DateTime start = DateTime.Now;
for(int i = 0; i< 1000000; i++)
{
//example expando...
public class ExtensibleClass : IFlyweight
{
private Dictionary<string, object> _props = new Dictionary<String, object>();
public object this[String key]{
get{
return this._props[key];
}
//Get the action names for a particular controller type.
public static IEnumerable<String> ActionsFromController(this Type controllerType)
{
return controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance).OfType<MethodInfo>()
.Where(y=>typeof(IActionResult).IsAssignableFrom(y)).Select(y=>y.Name);
}
//Makes a hex string from bytes.
public static String HexStringForBytes(this IEnumerable<bytes> bytes)
{
return bytes.Aggregate(new StringBuilder(), (seed,current)=>seed.AppendFormat("{0:x2}",current)).ToString();
}
public class Widget
{
public ObjectId Id {get;set;}
public String Color {get;set;}
public double Price {get;set;}
public DateTime ReleaseDate {get;set;}
public IEnumerable Reviews {get;set;}
}
//Next, spool up a connection to your database
//(The DB doesn't have to exist yet, but MongoDB DOES need to be running)
using Norm;
using Norm.Responses;
using Norm.Collections;
using Norm.Linq;
public class MongoSession {
private string _connectionString;
public MongoSession() {
public Post GetMostRecentPost()
{
Post mostRecentPost;
using(var db = Mongo.Create("mongodb://localhost/BlogApp"))
{
var posts = db.GetCollection<Post>();
//create a LINQ queryable to search the DB.
var q = posts.AsQueryable();
using (var db = Mongo.Create ("mongodb://localhost/testdb"))
{
var posts = db.GetCollection<Post> ();
//you can just put a lambda expression in for the fields that you want indexed.
posts.CreateIndex (j => new { j.Body, j.Title },
"Compound_Post_Index",true,IndexOption.Ascending);
}