Skip to content

Instantly share code, notes, and snippets.

View josheinstein's full-sized avatar

Josh Einstein josheinstein

  • Einstein Technologies
  • USA
View GitHub Profile
@josheinstein
josheinstein / AzureTableBatchHelper.cs
Last active August 29, 2015 14:19
When doing batch operations in Azure tables, you gotta be sure all entities are in the same partition. Here's some helpful, generally useful code for taking any IEnumerable of table entities and turning it into an IEnumerable of BATCHES of table entities. Nerd boner!
internal static class ExtensionMethods
{
/// <summary>
/// The largest batch size allowed by Azure table storage.
/// </summary>
public const int MaxBatchSize = 100;
/// <summary>
/// Given a sequence of <see cref="ITableEntity"/> objects, returns them in batches of up to 100 entities
@josheinstein
josheinstein / AzureTableStartsWith.cs
Created April 13, 2015 18:09
LINQPad example of using StartsWith on RowKey and PartitionKey properties in Windows Azure Table Storage.
void Main()
{
// Windows Azure Table Storage does not support a StartsWith function
// on string properties. However, it does support the CompareTo function.
// This will allow us to simulate the StartsWith function by taking the
// substring that you want to match on and figuring out the exclusive
// upper bound by incrementing the last character.
// In this example, Buildings is a table that uses a postal code as
@josheinstein
josheinstein / Write-Highlight.ps1
Last active August 29, 2015 14:14
Write-Highlight
#.SYNOPSIS
# Writes text to the host, highlighting portions of the text that
# match one or more regular expression patterns in a different
# color.
#
#.DESCRIPTION
# There are two forms that this command can take: One which specifies
# a single regular expression pattern and a single color (which defaults
# to yellow if not specified), and another that takes a hashtable that
# consists of regular expressions for keys and colors for values.
@josheinstein
josheinstein / Dialogs.psd1
Last active February 4, 2020 02:58
A PowerShell module for showing common dialogs. (Currently only supports open and save file dialogs.)
@{
# MODULE
Description = "Common Dialogs Module"
ModuleVersion = '1.0'
GUID = '374EF8CD-AEC6-464C-92DA-290C662DA183'
# AUTHOR
Author = 'Josh Einstein'
@josheinstein
josheinstein / MyDbContext.cs
Created April 16, 2014 20:07
When using Entity Framework code-first with LINQPad (or any time an EF DbContext is subclassed) any calls to Database.SetInitializer with the base class is effectively ignored. This is especially irritating with LINQPad which needs to subclass the context class in order to work the way it is designed. This means that if you called Database.SetIn…
public class MyDbContext : DbContext
{
/// <summary>
/// Initializes a new instance of the <see cref=“T:MyDbContext"/> class.
/// </summary>
public MyDbContext( )
: base( "DefaultConnection" )
{
@josheinstein
josheinstein / ParameterDefaults_Date.sql
Last active August 29, 2015 13:58
It isn't pretty, but it's ridiculously useful when you need to set a default value for a parameter in Microsoft SQL Server Report Builder. Simply use this view as the source of the default values and choose the column corresponding to the relative date you want to make the default.
CREATE VIEW ParameterDefaults_Date
AS
SELECT
CONVERT(date, GETUTCDATE()) AS [Today],
CONVERT(date, DATEADD(day, 1, GETUTCDATE())) AS [Tomorrow],
CONVERT(date, DATEADD(day, -1, GETUTCDATE())) AS [Yesterday],
CONVERT(date, DATEADD(day, 1-DATEPART(day, GETUTCDATE()), GETUTCDATE())) AS [ThisMonthStart],
CONVERT(date, DATEADD(day, -1, DATEADD(month, 1, DATEADD(day, 1-DATEPART(day, GETUTCDATE()), GETUTCDATE())))) AS [ThisMonthEnd],
CONVERT(date, DATEADD(month, 1, DATEADD(day, 1-DATEPART(day, GETUTCDATE()), GETUTCDATE()))) AS [NextMonthStart],
@josheinstein
josheinstein / Export-Excel.ps1
Last active January 22, 2023 17:02
A better way to pipe PowerShell output to an Excel spreadsheet with some basic support for formatting and much, much faster than using COM automation to populate the cells.
#.SYNOPSIS
# Exports objects to an Excel spreadsheet by writing them to a temporary
# CSV file and using Excel automation model to import it into a workbook.
# This allows formatting to be applied to columns which would not otherwise
# be possible in a plain CSV export.
function Export-Excel {
[CmdletBinding()]
param(
@josheinstein
josheinstein / NormalizeContactInfo.cs
Last active August 29, 2015 13:57
Sometimes I wonder how I ever got by in C# without the LINQ query syntax. Even though only a small part of this code is actually database-related, the composable nature of LINQ and the range variables make it so much cleaner than the equivalent code using primitive looping constructs. http://josheinstein.com/blog/2014/03/nothing-exciting-really-…
void Main()
{
// Query the OpsLog database for MACD records that have contact
// information in the record. A customer must also be associated
// with the record.
// No need to bother sorting here. That will happen in-memory in
// the next step.
var query = (
@josheinstein
josheinstein / VerifySessionStateFilter.cs
Created February 11, 2014 16:19
When your ASP.NET MVC login session depends on session state, annotating your controller with this attribute will ensure that the required session state is present. Otherwise it will attempt to re-load it or log the user out if it cannot be reloaded.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.Owin.Security;
@josheinstein
josheinstein / PasteVFC.vb
Last active December 30, 2015 00:59
Paste Values, Formats, and Comments.
' Steps to create:
' 1. Turn on the "developer" tab in the ribbon.
' 2. Copy some cell to the clipboard.
' 3. Click into an empty cell.
' 4. Click "record macro" on the developer tab.
' 5. Name it "PasteVFC" and choose to store it in "Personal Macro Workbook" (important)
' 6. Click "paste special" and choose "values & formats"
' 7. In the same cell, click "paste special" and choose "comments"
' 8. Click "stop recording" on the developer tab
' 9. Click "Visual Basic" on the developer tab and the macro code *should* look like below