Skip to content

Instantly share code, notes, and snippets.

@keithbloom
keithbloom / ReflectingAnonymousTypes
Created February 7, 2011 16:19
Using anonymous types to set test data.
[TestFixture, Explicit]
public class ReflectingAnonymousTypes
{
[Test]
public void DoesItWork()
{
StudentVisa student = Build.AStudentVisa();
var props = new {FirstName = "Keith", NationalityISO = "arj"};
SetProperties(student,props);
@keithbloom
keithbloom / DropAndCreateForiegnKeys.ps1
Created February 8, 2011 10:41
A powershell script which creates a couple of SQL files to drop and re-create foreign key constraints
#See http://msdn.microsoft.com/en-us/library/cc281962.aspx
PowerShell -NoExit -Command "$HOME\Documents\WindowsPowerShell\InitializeSQLProvider.ps1"
cd SQLSERVER:\sql\`(local`)\SQLSERVER2008\Databases\AdventureWorks2008\Tables
$opts = New-Object Microsoft.SqlServer.Management.Smo.ScriptingOptions
$opts.ScriptDrops = $true
$tables = ls | where {$_.Schema -eq "Production"}
$tables | foreach {$_.ForeignKeys} | foreach {$_.Script()} >> C:\Temp\CreateKeysScript.sql
@keithbloom
keithbloom / SelctWithIndexValue.cs
Created February 25, 2011 13:59
LINQ Select query which includes the iterator index in the projection
var lines = new[]
{
"Line1", "Line2", "Line3"
};
var query = lines.Select((line, index) => new
{
Prop = string.Format("Address{0}", index + 1),
Value = line
});
public void ImportFiles()
{
var query = from f in files
where f.Extension.ToLower() == ".zip"
select new ImportContext
{
ZipFileName = f.FullName,
Path = UploadPath
};
@keithbloom
keithbloom / gist:1048488
Created June 27, 2011 08:14
so_6469804
<html>
<head>
<script type="text/javascript" language="JavaScript">
function addCommas(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
@keithbloom
keithbloom / eilon-lipson-GetProperties
Created September 20, 2011 14:01
blog-post-using-iterators
private static IEnumerable<PropertyValue> GetProperties(object o)
{
if (o != null) {
var props = TypeDescriptor.GetProperties(o);
foreach (PropertyDescriptor prop in props) {
var val = prop.GetValue(o);
if (val != null) {
yield return new PropertyValue { Name = prop.Name, Value = val };
}
}
@keithbloom
keithbloom / .gitignore
Created October 6, 2011 10:40
Remove files which should be ignored from a git repsoitory
ld/
*.suo
*.user
bin
Bin
obj
_ReSharper*
*.csproj.user
*.resharper.user
*.suo
@keithbloom
keithbloom / Default.aspx.cs
Created October 7, 2011 14:34
WebFromsAndSRP
using System;
using WebFormsAndSRP.CookieManagement;
namespace WebFormsAndSRP
{
public partial class Default : System.Web.UI.Page
{
private IMarketingTracker _marketingTracker;
protected void Page_Load(object sender, EventArgs e)
{
@keithbloom
keithbloom / example1.sql
Created October 27, 2011 07:03
TopN Stored Procedure
SELECT TOP 5 description, hit_count
@keithbloom
keithbloom / example1.sql
Created October 27, 2011 07:36
ExcelInSql
SELECT u.[region 1], s.full_name AS RegionalManager
FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0', 'Data Source=c:\managers.xls;Extended Properties=Excel 8.0')...Updates$
JOIN staff s
on s.id = u.staff_id