Skip to content

Instantly share code, notes, and snippets.

@jonsagara
jonsagara / Program.cs
Last active February 1, 2018 17:09
How many Friday the 13ths occur in a year?
void Main()
{
var startYear = 2000;
var endYear = DateTime.UtcNow.Year;
var spookyYears = new List<(int year, int occurrences)>();
for (var ixYear = startYear; ixYear <= endYear; ixYear++)
{
var spookyDaysThisYear = 0;
@jonsagara
jonsagara / CreateMediatRClasses.xml
Last active February 4, 2025 23:40
Inserts Query, Command, CommandResult, QueryHandler, and CommandHandler classes for MediatR 5.x. Save with the extension .snippet instead of .xml.
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>CreateMediatRClasses</Title>
<Author>Jon Sagara</Author>
<Description>Inserts Query, Command, and QueryHandler classes for MediatR</Description>
<Shortcut>mediator</Shortcut>
</Header>
@jonsagara
jonsagara / QuickProfiler.cs
Last active September 21, 2016 21:14
Quick-and-dirty C# profiler. Really, it's just a wrapper for Stopwatch. Replace Console.WriteLine with whatever output makes sense.
public class QuickProfiler : IDisposable
{
private readonly Stopwatch _sw = Stopwatch.StartNew();
private readonly string _name;
public QuickProfiler(string name)
{
_name = name;
}
@jonsagara
jonsagara / Examples.cs
Created September 4, 2016 15:59 — forked from NickCraver/Examples.cs
SQL Exception handy helpers
// Here are some example usages for unimportant jobs we have where crap happens occasionally:
/// <summary>
/// intended for database commands that might deadlock, but are just "nice to haves"; we don't care if they deadlock every now and then
/// and we DON'T want them to block execution of the rest of /daily or /hourly! this returns -1 if deadlocked, otherwise, returns
/// the # of rows that the SQL command affected
/// </summary>
private int ExecuteIgnoreDeadlocks(string sql, object param = null, bool logDeadlock = false)
{
try
@jonsagara
jonsagara / AnyKeyQuit.xml
Created October 30, 2014 16:30
Snippet for a "Press any key to quit..." prompt in a Visual Studio C# console application
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>AnyKeyQuit</Title>
<Author>Jon Sagara</Author>
<Description>Inserts a &quot;Press any key to quit...&quot; prompt</Description>
<Shortcut>quit</Shortcut>
</Header>
@jonsagara
jonsagara / OpmlStats.cs
Created May 24, 2014 17:31
Count OPML folders and feeds
using System;
using System.Linq;
using System.Xml.Linq;
namespace OpmlStats.ConsoleApp
{
class Program
{
private const string OpmlFile = @"C:\Path\To\Your\OpmlFile.opml";
@jonsagara
jonsagara / AddTempdbFiles.sql
Created January 18, 2014 21:00
Add more files to tempdb, make them the same size, and use a fixed size for autogrowth. This is especially important when READ_COMMITTED_SNAPSHOT, which causes SQL Server to the sh*t out of tempdb. Be sure to restart SQL Server after making this configuration change.
USE [master]
GO
ALTER DATABASE [tempdb] MODIFY FILE ( NAME = N'tempdev', SIZE = 102400KB , FILEGROWTH = 102400KB )
GO
ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev2', FILENAME = N'D:\Path\To\Your\DATA\tempdb2.mdf' , SIZE = 102400KB , FILEGROWTH = 102400KB )
GO
ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev3', FILENAME = N'D:\Path\To\Your\DATA\tempdb3.mdf' , SIZE = 102400KB , FILEGROWTH = 102400KB )
GO
ALTER DATABASE [tempdb] ADD FILE ( NAME = N'tempdev4', FILENAME = N'D:\Path\To\Your\DATA\tempdb4.mdf' , SIZE = 102400KB , FILEGROWTH = 102400KB )
GO
@jonsagara
jonsagara / iPhoneMediaSorter.cs
Created October 13, 2013 23:26
Copy a flat directory of iPhone pictures and videos into a YYYYMM folder structure, based on the date the picture/video was taken.
class Program
{
private const string SourceDirectory = @"D:\iPhoneMedia";
private const string DestDirectory = @"D:\iPhoneMediaSorted";
static void Main(string[] args)
{
var sourceDir = new DirectoryInfo(SourceDirectory);
// Group by YYYYMM
@jonsagara
jonsagara / SqlVersionToByteArray.cs
Created July 3, 2013 15:55
Convert a SQL Server version to a byte array in C#
var hexNum = "0x000000000000E295";
long result;
long.TryParse(
hexNum.Substring(2),
System.Globalization.NumberStyles.HexNumber | System.Globalization.NumberStyles.AllowHexSpecifier,
System.Globalization.CultureInfo.CurrentCulture,
out result
);
//result.Dump();
var resultBytes = BitConverter.GetBytes(result);
@jonsagara
jonsagara / LinqExtensions.cs
Created September 16, 2012 17:16
IQueryable<T> Extensions
public static class LinqExtensions
{
public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, bool isAscending)
{
return isAscending
? source.OrderBy(keySelector)
: source.OrderByDescending(keySelector);
}
public static IOrderedQueryable<TSource> ThenBy<TSource, TKey>(this IOrderedQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, bool isAscending)