Skip to content

Instantly share code, notes, and snippets.

View chilversc's full-sized avatar

Chris Chilvers chilversc

View GitHub Profile
@chilversc
chilversc / gist:4224461
Created December 6, 2012 13:32
reading xml fragments from a stream
void Main()
{
var stream = new StringReader("<begin><test>1</test> <test>2</test> <test>");
var reader = XmlReader.Create(stream);
// find the root element
MoveToNextElement(reader);
// find the first content element
MoveToNextElement(reader);
@chilversc
chilversc / gist:4236568
Created December 7, 2012 21:11
MSSQL MERGE runs update triggers
-- This behavior is correct, just surprising if you havn't designed your trigger correctly.
-- Standard cause is designing a trigger that expects exactly 1 row and only 1 row to be changed
-- and doesn't account for if no rows are updated, or more than 1 row is updated.
create table foo (id int primary key, value nvarchar(255));
go
create trigger t_foo_update on foo for update as
set nocount on
@chilversc
chilversc / gist:5047785
Created February 27, 2013 13:08
Couple of VS macros, change insert tabs/spaces, removing trailing spaces and attach debugger to IIS Express
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module Module1
Sub UseTabs()
@chilversc
chilversc / gist:5082299
Created March 4, 2013 13:41
Use jquery mobile with requirejs, delay pageinit function until scripts are loaded
<script src="/scripts/require.js" type="text/javascript"></script>
<script type="text/javascript">
requirejs.config({
paths: {
'jquery': 'jquery-1.9.1.min',
'jquery.mobile': 'jquery.mobile-1.3.0.min'
},
shim: {
'jquery.mobile': ['jquery', 'jquery.mobile-config']
}
@chilversc
chilversc / gist:5100296
Created March 6, 2013 15:52
NHibernate Contains query bug - mutating a collection after a query has executed breaks queries in subsequent sessions
[Test]
public void UsersWithListContains_MutatingListDoesNotBreakOtherSessions()
{
using (var firstSession = OpenSession ()) {
var names = new List<string> { "ayende", "rahien" };
var query = (from user in firstSession.Query<User> ()
where names.Contains(user.Name)
select user).ToList();
@chilversc
chilversc / README.md
Last active December 15, 2015 11:48
Format observable as a number with a specific precision
@chilversc
chilversc / Example.cshtml
Last active December 16, 2015 03:39
Simple indexed prefix for razor foreach loop
@foreach (var item in Html.Prefix(Model.Results, "result")) {
<label for="@Html.Id("comment")>Comment</label>
@Html.TextArea("comment", item.Comment)
}
@using (Html.Prefix("test")) {
<label for="@Html.Id("nested")">Nested</label>
@Html.TextArea("nested")
}
@chilversc
chilversc / gist:5575617
Created May 14, 2013 12:47
Connection to MySql from .net over an SSH tunnel, using http://nuget.org/packages/SSH.NET/
void Test()
{
var ci = new ConnectionInfo ("remoteserver", "remoteuser", new PasswordAuthenticationMethod ("remoteuser", "password"));
var cs = new MySqlConnectionStringBuilder ();
cs.AllowBatch = true;
cs.Server = "127.0.0.1";
cs.Database = "database";
cs.UserID = "dbuser";
cs.Password = "dbpassword";
@chilversc
chilversc / gist:5643278
Created May 24, 2013 12:49
Specify field order for CsvHelper when writing
static void WriteCsv<T> (IEnumerable<T> data, string file, params string[] fields)
{
var writerType = typeof (CsvWriter);
var writerParam = Expression.Parameter (writerType, "writer");
var itemType = typeof (T);
var itemParam = Expression.Parameter (itemType, "item");
var fieldWriters = new List<Action<CsvWriter, T>> ();
foreach (var name in fields) {
var value = Expression.PropertyOrField (itemParam, name);
@chilversc
chilversc / gist:5731769
Created June 7, 2013 19:30
Helper class to allow comparing part of a key in linq groupby/join operations using a specific comparer
/* Usage:
source.GroupBy (x => new {
x.TeamId,
x.Name.CompareUsing (StringComparer.OrdinalIgnoreCase)
})
*/
public struct KeyPart<T> : IEquatable<KeyPart<T>>
{
private readonly IEqualityComparer<T> comparer;