JavaScript like Arrays in C#

This is a hack and I know it is but I like how easy it is.

I've been doing a ton of JavaScript lately and this has been bleeding into my C#. I needed to check values in a set during a LINQ query against a small set and came up with

new [] { 1, 2, 3 }.Contains(value)

That did the trick!

Now, would I deploy this in production? Definitely not. I can't put it in a constant so my best bet would be a static array. No sense building up and destroying the array every time the function is called.

Still a neat little trick.

jQuery Input Masks

This Input Mask jQuery plugin has been very useful for me. It just works and and the minified code weighs in at 4k.

John Bush's Masked Input Plugin creates masked inputs using the simple jQuery you've come to know and love. I added a phone number (U.S.) mask to my entire form using this single line of code:

$(".phone").mask("(999) 999-9999");

Thanks to John Bush for making this available.

LINQ to SQL Strongly Typed Stored Procedures

One of the issues I have run into is how to get multiple stored procedures to return a single type using LINQ to SQL. I have seen several methods that edit the Xml of the dbml by hand to do this. This is problematic as your changes revert as the dbml is modified. I found this solution online (although I can’t remember where. Thanks to the original source that I cannot properly credit).

The solution is a class defined in the dbml to serve as the Return Type of the stored procedure. That’s all there is to it.

The Stored Procedure
I’ll be using the adventure works database for this example. If you need this db you can download it here: http://msftdbprodsamples.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=37109

In particular I wanted a stored procedure that returned composite data. I chose the uspGetManagerEmployees function for its high content vs. low arguments appeal.



The Project
The example application is a simple console application. Create one and accept the defaults. Once VS 2008 has finished loading, right click the project and select Add -> New Item (Crtl+Shift+A).



Select the LINQ to SQL Classes template. I have chosen to name mine DB.dbml. Visual studio will show the LINQ to SQL diagram waiting for your SQL Tables and Procedures. In order to add items to the diagram we need a database connection. Open the Server Explorer (Crtl+Alt+S). Now you need to add a Data Connection if you don’t already have one. Click the Connect to Database button on the Server Explorer (Sorry no default shortcut).



Here you can set your server settings. Once you have a successful test you click OK. This will save the connection information to the app.config file.

Now it’s time to add our Return Type to the diagram. Right click the design surface and select Add -> Class. Select the default class name Class1 and rename it to ManagerEmployee.



Right click the class designer and select Add -> Property. Rename this property to RecursionLevel. This is easier of you use the property window (F4). Change the type of the RecursionLevel to int. Add the rest of type as listed below:



ManagerID int
ManagerFirstName string
ManagerLastName string
EmployeeID int
FirstName string
LastName string

When you are done you should see this:



The next step is to add your stored procedure. Expand the database in the server explorer until you find uspGetManagerEmployees. Select and drag this procedure to the Methods Pane of the diagram. Return to the properties (F4) and change the name of the procedure to GetManagerEmployees. Change the Return Type from (Auto-generated Type) to ManagerEmployee by selecting it from the list. Note: the source of the procedure still points to uspGetManagerEmployees.



Now you can save the dbml. When you do this you will get this prompt. Say Yes to it to make it go away. This is notifying you that the Return Type will be reset to (Auto-generated Type) only by deleting it and re-adding it to the Method Pane.



Once the save operation is complete if you open the DB.designer.cs file you will see the code generated to support your LINQ stored procedure. The part that relates is listed below:



So now we have a nice strongly typed class that can be used by any number of procedures. I have included a simple example to show its use below:



And here are the results:



Well there it is. Even though it is a simple solution, since it is the LINQ to SQL way it persists your changes across edits and prevents hand edits of the source xml.

Simpsons Search Engine Connection

I posted this one a year ago on my home page. I wanted to repost here to keep everything together.

So I'm watching the Simpsons the other night. The episode is Colonel Homer. This is the one where Homer becomes the manager of Lurleen the country singer. At the start of the episode the family goes to the Googolplex to watch a movie.

Think about this, this show came out in 1992. Even with the mathematical connotation aside this may have been an early seed planted in the Google founders minds. Does anyone know if those guys watch the Simpsons?

Ok so that in itself would be cool. But, in that same episode, Lurleen appears on the HeeHaw knockoff Ya Hoo! Now this is just too much!

How likely is it that Googol (the original corporate spelling) founded 1998 and Ya Hoo founded 1994 would appear together in the popular media and later become 2 of the most powerful web companies in the world.

I personally will be naming my next business out of something from that episode.

Top Contenders:
Beer and Brawl
Lurleen

Hello World

Welcome to my blog. The purpose of this blog is to take a brain dump of some of the programming tasks I find interesting. Additionally I have a requirement in my employment contract to publish technical articles and this satisfies it nicely.

I have a post ready and will post it soon. I deals with binding a stored procedure against a strongly typed class in Linq To SQL. It's coming up on the long Christmas weekend so I should have time to get it posted.

Thanks for reading.