Skip to content

Instantly share code, notes, and snippets.

View nonnb's full-sized avatar

Stuart Corrans nonnb

View GitHub Profile
OP needs to dynamically find a filtered row count across (almost) tables in a database, based on a master reference table
containing the column names. OP then wants to further restrict this result set by seeking only specific values of the result.
AFAIK this can only be done with dynamic SQL. Here I build up a nasty dynamic sql query which consists of a CTE unioning
all the grouped table row counts meeting the master table criteria, and then applying an overall grouped sum at the bottom.
@nonnb
nonnb / ChickenAndEggPrimesGenerator.cs
Last active March 1, 2017 08:29
Generators, Iterators
using System.Collections.Generic;
using System.Linq;
namespace Generators
{
public static class PrimesGenerator
{
public static IEnumerable<int> GetPrimes()
{
// Given just 2, we can determine all other primes
@nonnb
nonnb / NinjectAlternativeToAbstractFactory.cs
Created March 12, 2017 10:49
StackOverflow21285090 - Alternatives to Abstract Factory pattern using Ninject
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Ninject;
using Ninject.Modules;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
// Original Code:
// public static bool IsNull_yes(dynamic source)
// {
// if (source.x == null || source.x.y == null) return true;
// return false;
// }
// Decompiled with dotPeek
public static bool IsNull_exception(object source)
{
@nonnb
nonnb / SneakyThrowExAntiPattern.cs
Last active February 24, 2020 17:04
EnrichingExceptions and retaining StackTrace
void Main()
{
try
{
// We want the stacktrace to drill through here.
BuggyMethod();
}
catch (Exception ex)
{
// Direct Mutation of the exception reference doesn't reset the stacktrace, although may be dubious practice (wrapped InnerException is arguably a better, mutation free alternative)
@nonnb
nonnb / NUnitCaseName.cs
Last active December 17, 2021 11:34
NUnit - Adding Test Case Name to Data Driven Class Data
using System.Collections.Generic;
using NUnit.Framework;
namespace NUnitNames
{
public class Tests
{
private static readonly IEnumerable<MyTestModel> MyTestModels = new[]
{
new MyTestModel
@nonnb
nonnb / XUnitTestCases.cs
Created December 17, 2021 11:37
XUnit - Test Runner to show descriptive name for Use Case
using System.Collections;
using System.Collections.Generic;
using Xunit;
namespace TestNames
{
public class UnitTest1
{
[Theory]
[ClassData(typeof(MyTestModel))]