LinqPad
If you are not sure what LinqPad is or why you should be using it, take a moment and go check it out.
I use LinqPad all the time. As a .NET developer, it is my go-to scratchpad for spike development and has almost completely removed my dependency upon SQL Server Management Studio for data analysis and query construction. Since I use LinqPad all the time, I have stumbled upon a few things to make my life easier when dealing with connection information in referenced code and data analysis of stored procedure result sets within Linqpad. Hopefully by jotting these down here, I’ll be able to find this information easily next time.
Overriding ConnectionString Information
While this isn’t really LinqPad specific, I recently used this solution to overwrite the connection information used by a bit of internal code we were referencing with the current LinqPad connection. First, we need to tell our configuration collection that all of the connection strings should not be read only. To do this we need to find the “Read Only” property in the ConfigurationManager.ConnectionStrings collection and set that propery to false so we can overwrite that information with our custom definition:
1
2
3
4
5
// **Note: This relies on the property name "bReadOnly".
// If this property name is changed this will no longer work.
typeof(ConfigurationElementCollection)
.GetField("bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic)
.SetValue(ConfigurationManager.ConnectionStrings, false);
Now that we can manipulate the connection string collection, we need to locate the one used by the code we are refrencing and replace it with the current connection within LinqPad.
1
2
3
4
5
6
7
8
//Add the current LinqPad connection string with the name
//"myConnection" which is what my referenced code uses to connect
ConfigurationManager.ConnectionStrings
.Add(
new System.Configuration.ConnectionStringSettings(
"myConnection",
this.Connection.ConnectionString)
);
Filtering Stored Procedure Results
After executing a stored procedure within LinqPad, I wanted a way to filter through the results for a specific item in order to retrieve my value of interest. I decided to use the generic Field method to pull the value I wanted to filter by for each record in the dataset. This allows me to extract what I want from the dataset using a simple Where clause.
1
2
3
4
5
//Call the Stored Procedure and access the dataset
var data = usp_MyStoredProc (param1, param2).Tables[0];
data
.AsEnumerable()
.Where(x => x.Field<int>("Id") == 1234).Dump();
Useful Extensions
Here is a list of some other useful extensions.