Generic Insert and Update for LINQ To SQL

Quick code snippet time! The following are generic methods for inserting and updating a detached entity into a database using LINQ to SQL. 1: /// <summary> 2: /// Updates the database with item. 3: /// </summary> 4: /// <typeparam name="T">Type of the item</typeparam> 5: /// <param name="item">The item.</param> 6: public static void UpdateDatabaseWithItem<T>(T item) where…

2

Worst Case Scenario for QuickSort

Take a look at the following code: 1: var sw = new Stopwatch(); 2: sw.Start(); 3: Enumerable.Range(0, 3).SelectMany((i) => Enumerable.Range(0, 50000)).OrderBy(i => i).ToList(); 4: Console.WriteLine(sw.ElapsedMilliseconds); 5:  6: sw.Reset(); 7: sw.Start(); 8: Enumerable.Range(0, 2).SelectMany((i) => Enumerable.Range(0, 50000)).OrderBy(i => i).ToList(); 9: Console.WriteLine(sw.ElapsedMilliseconds); 10: sw.Reset();   Line 3 is saying: Generate a list which looks like 0,1,2..49999,0,1,2..49999,0,1,2..49999 Then…

2

The two most important WPF Tools

I have been working a lot with WPF and I found the following two FREE tools to be extremely helpful. The first is: XamlPadX – (http://blogs.msdn.com/llobo/archive/2007/12/19/xamlpadx-v3-0.aspx) This is an enhanced version of XamlPad which comes with the .NET SDK.  This is a light weight tool which parses and renders XAML code visually.  It adds (in…


Foreach is Duck Typed!

I thought I know how the foreach construct worked under the covers. I figured the compiler would check if the type being iterated over implement IEnumerable or IEnumerator.  And if so it will call MoveNext and Current to loop over the elements.  But then I read this: http://blogs.msdn.com/kcwalina/archive/2007/07/18/DuckNotation.aspx In that post Krzysztof reveals that in…

5

Writing a Regular Expression parser in Haskell: Part 4

With the previous two modules in place we are now set up to use a DFA to match against a string.  In my implementation I support either a greedy match or an short match.  In a full featured regular expression engine this ability to choose greedy or not would be per operator but for simplicity…

1

Writing a Regular Expression parser in Haskell: Part 3

The third module in the simple regular expression parser is called: NFAtoDFA.  Which as you might have guessed, takes the NFA that resulted from the first module and converts it into a DFA.  The structure that the DFA uses is the same that the NFA uses since they are both finite state machines. Converting an NFA…

1

Writing a Regular Expression parser in Haskell: Part 2

The first module in my simple regular expression parse is called RegexToNFA.  This module exposes the types that make up a finite state machine and also the functions to convert a regular expression string into a finite state machine.My structure for a FSM follows closely from the mathematical definition: 1: data FiniteMachine = FiniteMachine{ table…

1

Writing a Regular Expression parser in Haskell: Part 1

A few weeks ago I read this article about writing a simple regular expression parser.  That article does a really good job of explaining the theory behind regular expression.  It then goes step by step into how to write a program (he uses C++) to parse a regular expression, convert it into a NFA, convert…


ArgumentNullException vs ArgumentException

Both ArgumentNullException and ArgumentException have a constructor which takes two strings.  One is the name of the parameter (or argument) in question and the other is a string describing the exception. The funny/odd/interesting thing about them is that one has the opposite order of arguments. For ArgumentException the constructor is: 1: public ArgumentException( 2: string…

2

Intro to LINQ to SQL Optimistic Concurrency

After some investigation I feel I have a decent understanding on how LINQ to SQL concurrency works. LINQ to SQL uses optimistic concurrency which means that it never takes an exclusive lock on a table.  It caches the information you are working with and then (by default) when you submit changes verifies that the table…