This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
public class MyClass | |
{ | |
public static void RunSnippet() | |
{ | |
DateTime start = DateTime.Now; | |
for(int i = 0; i< 1000000; i++) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Norm; | |
using Norm.Responses; | |
using Norm.Collections; | |
using Norm.Linq; | |
public class MongoSession { | |
private string _connectionString; | |
public MongoSession() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
OlderNewer