Visual Studio Related Posts by ThinqLinq

Enabling LINQ to SQL in Visual Studio 2017

One of the benefits that Visual Studio 2017 brings is a light weight installer. Instead of spending hours to download and install Visual Studio as was typical in the past, the Visual Studio team worked hard to componentize the installation process and limit the amount of components you need to install for typical development tasks. This makes the installer take minutes rather than hours as we were accustomed to in the past.

The down side is that some features that you may have been used to out of the box now require separate manual installs. This includes two tools I use frequently including the Class Designer and LINQ to SQL tools. If you used these in the past and can’t find them now, fire up the Visual Studio Installer again and click the “Modify” button.


The workloads tab appears by default, but this won’t help enable the tools mentioned above. For those, you need to click the “Individual Components” tab and scroll down to the “Code tools” section. From there you can select and install the LINQ to SQL, Class Designer, and a host of other useful tools.


As you can see from the screen shot above, these screens and instructions apply to the Release Candidate. The actual screens and steps from the final version may change as the product release is finalized. Your mileage may vary.

Posted on - Comment
Categories: Visual Studio -

Microsoft Connect conference links and key takeaways

imageThis week, Microsoft ran an online conference which included a number of significant announcements. In case you missed it, the sessions are available for streaming on demand on Channel9. The major announcements were accompanied by a number of blog posts. In this post, I wanted to summarize the most important items that I noticed and include links to the detail pages where appropriate.

.Net Core open source – Perhaps the most significant announcement was the open sourcing of the .Net core. This does not mean that the entire framework is open sourced, but represents a leaner core part of .Net upon which other components can be added (including ASP.Net, WPF, Windows Forms, Entity Framework, etc). The intent here is to open the development process and encourage a more active and engaged open source community. To that end, the team accepted it’s first pull request within the first hour of the announcement. Perhaps the most ironic post regarding the open source announcement was @PVandenheede’s statement,

".NET open source? On Mac and Linux? Hah! Only when spacecraft start landing on freakin' comets!"

In addition, the new framework will shift from numbered versions to year names and it will be installed local to your solution rather than installing to the GAC that we’ve had a love-hate relationship with the original 1.0 release came out 12 years ago.

Visual Studio versions – Closely tied to the .Net version information, Microsoft also announced two new versions of Visual Studio. The Visual Studio Community Edition will contain the functionality of VS 2013 Pro (including supporting plugins) but will be free for open source, charity, students and companies with under 250 employees and 1 million annual revenue. This will potentially replace the express versions and reduce the need for the BizSpark and DreamSpark programs.

In addition, Microsoft officially named the next version as Visual Studio 2015 many of the new features that I will talk about below are also included in the release notes.

•ASP.Net vNext – Among the many features of VS 2015, perhaps the most significant is ASP.Net vNext including MVC 6 and EF 7. This version is a drastic re-thinking of the ASP stack including a JSON based project and configuration files, direct inclusion of Grunt, Bower, and a revised Nuget. The new .Net core is only 15 meg and can run natively not only on Windows, but also natively on OS X and Linux servers. At this point, I wouldn’t recommend writing production code in vNext, but it is worth keeping an eye on and kicking the tires a bit.

Entity Framework 7 – Perhaps even less stable that ASP.Net vNext, is the completely re-written EF7 which among other things removes the use of the EDMX  and reduces the API surface from previous versions. It is such a large change, that the team even posted recently the as to whether EF should be a v7 or v1 of a different product.

Typescript 1.3 – Typescript continues to mature and quietly updated to version 1.3 with the 2015 release. This version takes advantage of the Roslyn language services to support a number of new analytics and refactorings. If you want to configure ASP.Net vNext to automatically transcode your Typescript into JavaScript, check out  Marcin Juraczek’s post on configuring grunt-typescript.

imageCross platform – These days of Mobile first application development, most companies are trying to figure out how to reduce costs and increase code reuse. Visual Studio 2015 offers a number of new features to try to address these concerns.

Xamarin – Visual Studio 2015 includes increased partnership with Xamarin which allows developers to write C# code to natively target iOS, Andriod, Windows Phone, and Windows RT using a single code base.

Apache Cordova support – VS 2015 also includes enhanced tooling for creating mobile apps with HTML/JavaScript using the open source Apache Cordova (PhoneGap) tools including debugging iOS directly from Visual Studio.

Cross platform C++ – Microsoft also added support for writing cross platform C++ if that’s your language of choice or you need access to device native features that aren’t available using the other platforms.

•Andriod emulator – The official Andriod emulator from Google is widely acknowledged as being quite slow and problematic. Microsoft now offers an alternative emulator based on HyperV similar to their own Windows Phone emulator. – Not only can you deploy your ASP.Net vNext applications on Linux or OS-X, you can now use your favorite text editor to create the code in Sublime/Emacs/etc. Instructions on how to hook up your favorite editors are available at

Visual Studio 2015

Instead of detailing the changes in VS 2015, below is a quick summary of some of the more interesting ones that I found. Drill into each hyperlink below for details on the feature you’re interested in.

  1. New compiler for C#/VB(. Net Compiler Platform) (what’s new vids)
  2. New language features
  3. Code focused IDE enhancements
  4. Lightbulb refactorings (extensible)
  5. PerfTips
  6. Memory usage tool
  7. LINQ/Lambda expressions in debug/watch windows
  8. Bigger and filterable error list
  9. WPF roadmap tooling and debugging enhancements for WPF
  10. Auto generate smart unit tests
  11. .Net Native

VS OnlineimageWhether you prefer TFS or Git, you can use VS Online. I’m not going to try to summarize the new features here since they stated that they update the site every 3 weeks. By the time you read this, there is a high likelihood that they may have already added more features.

Application insights – One new feature worth mentioning is the Application Insights that can allow you to add analytics and diagnostic monitoring to your app and offer a helpful dashboard to use directly inside of VS Online.

Did you watch the Connect sessions and noticed something important that I missed here? Feel free to let me know what you thinq below.

Posted on - Comment
Categories: Visual Studio -

Tools and Apps

Now that I’ve been at Slalom Consulting for 3 years, it’s about time for a hardware refresh. In preparation for that, and inspired by the recent list by Rocky Lhotka of top Win8/RT apps, I thought I would share the list of software/tools that I have found indispensible over the last several years creating software for customers. This is by no means as extensive as the impressive list of tools that Scott Hanselman puts out each year. A while back, I published a list of LINQ tools if you’re looking for some of those.

This list is primarily intended as a list to help me when I build my next laptop and offered here in case you find it helpful as well. It is by no means an endorsement of any specific product and I’m not paid to publish any of these. Feel free to add to this list in the comments as I may well have forgotten something along the way.

System apps and tools

Windows Home Server I use this to seamlessly backup all of the computers on my home network. It’s saved my rear multiple times and makes trying out new builds of Windows and other applications a painless task. I’m still using the original version and it’s been grinding along smoothly for years now.
Windows Live Writer The best tool I’ve found for offline creating/editing of blog posts. So great, I’m using it to write this post.
Paint.Net Impressive free image editor that includes many of the features of Photoshop without the license costs. Includes multiple layers and special effects.
WinDirStat Do you ever wander what’s taking up so much space on your drives? WinDirStat is a nifty way to visualize your space usage and can break it down by folder/file type/etc. It’s an easy way to find those screen capture videos, vhd images, and audio files that can get scattered around the drive over time.
TouchFreeze This simple utility blocks the annoying mouse jump that can happen if your thumb accidently brushes the touchpad while typing.
Syncfusion Metrostudio Over 2500 metro-style icon templates that are free (in exchange for giving them your email address that they will use to spam up) and customizable either as XAML or png images. I’ve used these a number of times when needing an icon for a customer and changed the colors to match their brand.
FileZilla The best free FTP client I’ve found yet.
EasyBCD GUI to manage your boot image. Helps out in a pinch when the boot sector gets corrupted or you want to configure dual boot to a VHD image for testing new OS images. It has a free non-commercial version as well as a minimal cost commercial version.
Oracle VirtualBox Free virtual machine tool. I’ve found this to be lighter weight and easier to configure than HyperV or VmWare. I’m sure there are features that it doesn’t include, but so far I haven’t had the need for the bigger tools except for running the Windows Phone emulator which does require HyperV.
ProcessMonitor Low level debugging tool for when you want to see in minute detail what your drives are doing. The information that it provides can be overwhelming, but I’ve found it useful for tracking down pesky file not found issues where a system is looking for a file in a different path than where it actually exists.
Metrotwit This has been my go-to twitter client for years. Unfortunately due to Twitter’s access token limit, they recently announced that they will no longer support new users or upgrades. I may have to switch back to Tweetdeck or Tweetium once the new computer comes but wanted to leave this on my list in tribute to the app that was.
Foxit Reader Much smaller and faster than Adobe Reader. Handles 95% of my pdf reading needs with less overhead.

Development Tools

Visual Studio The best development IDE out there. I spend most of my time working in the .Net world, but have had to work with a couple others including Eclipse and am much happier and productive with VS than the other tools.
SQL Server Management Tools These are the tools for working with SQL Server including SSMS/ SSDT/ SQL Profiler and Tuning Advisor.
LINQPad Any time I want to test out some code snippets but don’t want to crank up a new project and build a UI, I reach out for LINQ Pad. We liked it so much that we published all of the LINQ in Action samples for you to use in LinqPad.
ILSpy Although not as full featured as the no longer free Reflector, I reach out to the open-source .NET assembly browser and decompiler, ILSpy whenever I want to know how a .Net DLL works under the covers and I don’t have access to the original source code.
WebMatrix If I’m doing a simple site and don’t need the full Visual Studio experience, I often reach out to the free WebMatrix as a web development sandbox.
Dependency Walker This simple tool lets you check the project dependencies of compiled modules which is helpful when trying to understand how existing systems are constructed so that we can try to extend them with additional functionality.
Tortoise SVN If I’m doing work for a client that uses SVN as their source repository, I often use this to integrate the source repository directly into windows explorer.
GitHub for Windows The easiest way to get started with GitHub on Windows computers without the need to fumble around with the command line.
Fiddler Anytime I need to sniff web requests or test services, Fiddler is my tool of choice. Not only are you able to see the requests and responses, but you can easily create new ones by dragging a previous request to the compose tab and modifying the URI/header/footers to meet your needs and issue the request directly without needing to mess with a browser.
WinMerge Often I need to compare files or directories to merge them between different versions. There are a number of diffing tools, but this one lets you quickly diff and merge them.

Visual Studio Extensions

I typically do not install many extensions including the popular CodeRush, Resharper, or JustCode because I’m frequently giving presentations and attendees often are more focused on the plug in tools that I’m using than the task at hand. As a result, I typically use a cleaner install of Visual Studio with just a handful of useful tools.

Nuget If you’re using Visual Studio and haven’t heard of Nuget, you’ve probably been living under a bridge for the last couple of years. NuGet is the way to manage external packages for your applications. Increasingly, parts of .Net including MVC, Entity Framework, Web API, and others are being distributed through NuGet rather than as part of large .Net version upgrades. It’s a must have extension for Visual Studio at this point.
Web Essentials Adds extensions into the web tools (including Html, javascript, css) that they couldn’t fit into the typical product cycles. There are versions for 2010, 2012, and 2013.
Chutzpah Test Runner Visual Studio plug in that allows you to run JavaScript QUnit, Jasmine, and Mocha unit tests the same way you run your MSTest/XUnit tests for .Net code—directly within Visual Studio. Also allows you to debug the JavaScript unit tests quickly and easily.
CssCop Integrates the CSSLint style checker into Visual Studio to help writing better CSS catching common errors that affect browser compatibility.
Azure SDK Tools and SDK’s for working with Azure services.
Fody Simple tool to weave code into your build process. In particular, I use this to add INotifyPropertyChanged events into POCO classes.
Posted on - Comment
Categories: Visual Studio -

CodeLens for Visual Studio 2013 configuration

One of the new features of Visual Studio is the ability to automatically see statistics about your code while you are looking at it via a new feature called CodeLens. For example, you can see how many method calls are referencing your method, how many bugs and what tests are accessing the method.


While this information can be helpful when reviewing code, it sometimes gets in the way when writing it. For example, you can’t just click the 0 references line and start adding attributes and comments, you have to add them in a blank line above the CodeLens injected text (included as images here because you can’t copy and paste the CodeLens text).


If you don’t want the CodeLens information getting in the way, simply right click one of the tags and select CodeLens Options. Alternatively, in the Quick Launch window (Ctrl+Q), just type CodeLens to find the menu in config. From here, you can tweak which values are displayed inline with your code or disable CodeLens entirely to get it out of your way while writing code.


Posted on - Comment
Categories: C# - Visual Studio -

Reactive Framework as a Background Worker

In this introduction to the Reactive Framework series, we’ve spent a bit of time setting up our Observable and Observers and wiring them up. If you haven’t been following along, here’s links to the previous posts:

So far, our observers can listen to our sensor, but it turns out, we can’t know about it because everything is happening on the main worker thread. Because the thread is continually processing, the UI locks us out of seeing the updates. In order to solve this, we need to run our sensor on a secondary thread.

With Reactive Framework, we often talk about “Hot” and “Cold” observables. Hot observables are ones which are running independently of the subscription. Cold observables are ones where the process starts when you subscribe to it. In our case, we’re simulating an ongoing sensor that we are connecting many observers to. In this case, we are dealing with a “Hot” observable. As a result, we’ll explicitly manage the sensor using the BackgroundWorker object in our “Start” button handler:

        Dim worker As New BackgroundWorker
        AddHandler worker.DoWork, Sub(s As Object, ars As DoWorkEventArgs)
                                  End Sub

Now, when we run our sample and output our results using Console.WriteLine, we see our results and we can continue to click on other buttons in our application. However, if we try to output the results to our user interface, we see the following exception:

     InvalidOperationException: The calling thread cannot access this object because a different thread owns it.

If you’ve ever worked with background threads in Windows Forms, WPF or Silverlight, you should recognize that you can’t access the UI thread from a background thread directly. One of the key scenarios that the Reactive Framework was designed to combat was asynchronous operations. As a result, they took great effort to make synchronizing these threads easy. Two of the extension methods on IObservable are SubscribeOn and ObserveOn. SubscribeOn is used indicate where the operations that we are subscribing to will be performed. ObserveOn is used to indicate where we want to process the results.

In our case, we need to move back to the UI thread when we process the results, thus we need to synchronize our threads when we Observe, thus we will use the ObserveOn option. To make matters easier, the Reactive team have included a special variant of the ObserveOn to synchronize it on the dispatching thread: ObserveOnDispatcher. We can alter our subscribing code as follows to make sure we observe our subscription on the UI Thread:

        Dim items = New ObservableCollection(Of Double)
        FilteredList.ItemsSource = items

        Dim TypeSensors = From s In Sensor
                       Where s.SensorType = "4"
                       Select s.SensorValue

            Sub(item) items.Add(item))

To see this sensor and various observables in action, download the corresponding WPF project for this series.

Posted on - Comment
Categories: VB Dev Center - Rx - Visual Studio -

Reactive Framework Building an IObservable Event Generator

In my last post, I mentioned a number of cases where you may want to use the Reactive Framework. For some upcoming presentations, I wanted to focus on a couple of these scenarios, particularly on how you can use the Reactive Framework (rX) to work with events from device sensors. You can often find these kind of sensors in a number of industries, including Robotics, automated manufacturing systems, Medical monitors, Telecom usage, and Live financial feeds. In order to demonstrate using rX in this environment, I needed to build a module that simulated generating a bunch of sample random events. Below is the module that I created. We’ll use this module in some of the future discussions of Reactive Framework.

We’re going to start with a small class that will contain the state of the individual sensor events. We’ll call this our SensorInfo class. It will hold values for the date and time that the event occurred, an indicator on the sensor’s type and the value that the sensor returns. We'll also override the ToString method to allow us to output the values easily.

Public Class SensorInfo
    Public Property TimeStamp As DateTime
    Public Property SensorType As String
    Public Property SensorValue As Double

    Public Overrides Function ToString() As String
        Return String.Format("Time: {0}  , Type: {1}  Value: {2}", TimeStamp, SensorType, SensorValue)
    End Function
End Class

Now that we have our instance class, we can create a class that will generate these sensor items randomly. (This class is not thread safe, nor is it truly random so don't use it in production applications. It is merely designed for demonstration purposes.)

Public Class ObservableSensor

    Private _running As Boolean

    Public Sub StartSensor()
        If Not _running Then
            _running = True
            Dim randomizer = New Random(Date.Now.Millisecond)
            While _running
                Dim randVal = randomizer.NextDouble
                Dim info As New SensorInfo With {.SensorType = CInt(randVal * 4).ToString(),
                                                 .SensorValue = randVal * 20,
                                                 .TimeStamp = Now}
                End If
                Threading.Thread.Sleep(CInt(randomizer.NextDouble * 500))
            End While
        End If
    End Sub

    Public Sub StopSensor()
        _running = False
    End Sub

End Class

In this class, we maintain an internal variable (_running) which tracks whether the sensor is running or not. We also have a method that Starts the sensor and stops it. While the sensor is running, we essentially generate a number of SensorInfo instances with randomized values and then pause for a random period of time before creating another value. At this point, the values that are returned don’t have much meaning. We could easily change this to return stock quotes, manufacturing defects or other sensor responses by manipulating the values this randomizer generates.

Now that we can generate random SensorInfos, we need to actually do something with them. In the past, we could just raise an event for consumers to handle after we generate each sensor’s value. Since we want to leverage the power of the new IObservable/IObserver interfaces and the Reactive Framework, I’ll make this class implement IObservable(Of T) so that we can register a number of IObserver clients and notify them each time we generate a new sensor.

The IObservable(Of T) interface requires of a single method: Subscribe. This takes a single parameter which is the IObserver client that wants to listen to our sensor data. It returns a class that implements IDisposable (so that we can make sure each of our observers know when we’re done sending them data). Since the return object here is actually the ObservableSensor itself, we need to implement both IObservable and IDisposable. Here's our revised ObservableSensor class.

Public Class ObservableSensor
    Implements IObservable(Of SensorInfo)
    Implements IDisposable

    Private _observers As New List(Of IObserver(Of SensorInfo))
    Private _running As Boolean

    Public Function Subscribe(ByVal observer As System.IObserver(Of SensorInfo)) 
                              As System.IDisposable 
                              Implements System.IObservable(Of SensorInfo).Subscribe
        Return Me
    End Function

    Public Sub StartSensor()
        If Not _running Then
            _running = True
            Dim randomizer = New Random(Date.Now.Millisecond)
            While _running
                Dim randVal = randomizer.NextDouble
                If _observers.Any Then
                    Dim info As New SensorInfo With {.SensorType = CInt(randVal * 4).ToString,
                                                     .SensorValue = randVal * 20,
                                                     .TimeStamp = Now}

                    _observers.ForEach(Sub(o) o.OnNext(info))
                End If
                Threading.Thread.Sleep(CInt(randomizer.NextDouble * 500))
            End While
        End If
    End Sub

    Public Sub StopSensor()
        _running = False
    End Sub

#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                If _observers IsNot Nothing Then
                    _observers.ForEach(Sub(o) o.OnCompleted())
                End If
                ' TODO: dispose managed state (managed objects).
            End If

            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    End Sub
#End Region

End Class

In this new version, we now have a new _observers object that maintains a list of the observers (clients). This allows us to notify multiple sensor handlers and work with them how they deem appropriate. The subscribe method simply takes the supplied observer and sticks it in the collection.

When we start the sensor, we now check to see if there are any observers (using the LINQ .Any method). If we do, we’ll generate the random sensor data. We then notify all of the listeners using the list .ForEach method passing the lambda expression instructing the observer to invoke it’s OnNext handler (part of the IObserver(Of T) implementation. This is the method which corresponds to IEnumerable’s MoveNext. It is this method which will trigger our reactive framework’s event pipeline to begin processing our sensor notifications.

When we’re done, we need to clean up our resouces. In the Disposing event, we make sure that we call the OnCompleted method on each (ForEach) of the observers in our _observers collection. We also clear the observer collection to remove the reference pointers between the client and our sensor generator.

There you have it, a generic random event generator that we can consume with the Reactive Framework (or similar technologies like StreamInsight). Next time, we’ll start to consume these events.

As always, let me know what you Thinq and if there are any modifications I should consider.

Posted on - Comment
Categories: Rx - VB - VB Dev Center - Visual Studio -

Visual Studio 2010 Keyboard Binding Poster

When I work with new teams, I often get asked how I did x using the keyboard instead of reaching for the mouse. I find if I can keep my hands on the keyboard, I can often be much more productive. So, how do you learn to take advantage of the keyboard shortcuts, I recommend downloading one of the Key binding posters like the ones that were just released for Visual Studio 2010. The posters are available for VB.Net, C#, F#, and C++.

Once you have then downloaded, pick a  a different key binding each day and dedicate yourself to using that one throughout the day. You’ll find some bindings work better for you than others. Each person has their favorite way of working, so try them all and see which are your favorites. So, what are you waiting for? Download them and get coding!

Posted on - Comment
Categories: Visual Studio - C# - VB -

Replace Foreach with LINQ

One of the best ways to start Thinqing in LINQ is to find places where you can replace iterative loops with LINQ queries. This won’t necessarily make your applications any faster at the moment, but by taking advantage of the declarative syntax, it will allow you to change your underlying implementation (ie. parallelize it with PLINQ) easier in the future. Recently, I came across some code that mimicked creating a control array as we used to have in VB6 by iterating over a list of fields and adding controls to a form adding the index to the end of the field name. As the fields are being added,we hook up event listeners along the way. Here’s the old code:

Dim Fields() As String = {"a", "one", "and", "a", "two"}

Dim index As Integer

For Each item As String In Fields
    Dim box As New TextBox
    With box
        .Name = item + index.ToString
        .Top = index * 30
        .Height = 24
        AddHandler .TextChanged, AddressOf OnValueChanged
    End With
    index += 1
To break this down, we're creating an index so that we won't have any repeats. We then iterate over our list of fields and create (Project) new textboxes for each of the items in our list. Once we create that value, we then add the handler. Finally we add this item to another list. If we think about this in a set based manner rather than iterative, we can start getting a grasp of what LINQ really has to offer. Let's rewrite this in LINQ starting with our source:

Dim Fields() As String = {"a", "one", "and", "a", "two"}
Dim b1 = From item In Fields
         Select New TextBox With
             .Name = item,
             .Top = 30,
             .Height = 24


In this example, we take our starting list. Project (Select) new objects from these values and then pass this list directly into the Controls collection using AddRange. No more For Each.

This is a start, but there's an issue. We need to be able to add the index to this set based operation. One of the little secrets in the LINQ operators is that there are overloads which expose the index. In VB, you can't access these using the LINQ query comprehensions. You have to use the extension methods and Lambda Functions directly as follows:

Dim Fields() As String = {"a", "one", "and", "a", "two"}
Dim boxes = Fields _
            .Select(Function(item, index) _
                New TextBox With {
                    .Name = item + index.ToString(),
                    .Top = index * 30,
                    .Height = 24})

Controls.AddRange(boxes.OfType(Of Control).ToArray)

We're almost there. We just need to add our handlers for each of our new text boxes. While we could call ForEach over an array, it would cause us to iterate over our field list twice (creating two sets of text boxes). We need a way to only iterate over it once. Here, we need to create a new method and using C# iterators. It will take an IEnumerable and return an IEnumerable. By using Yield, it will not cause the enumeration to happen multiple times, but rather to add a new step as each value is being pulled through the enumeration pipeline.

public static class Extensions
       public static IEnumerable<T> WhileEnumerating<T>(this IEnumerable<T> source, Action<T> action)
           foreach (T item in source)
               yield return item;

Now, we can inject methods into the pipeline as follows:

Dim boxes = Fields _
            .Select(Function(item, index) _
                New TextBox With {
                    .Name = item + index.ToString(),
                    .Top = index * 30,
                    .Height = 24}) _
            .WhileEnumerating(Sub(item) AddHandler item.TextChanged, AddressOf OnValueChanged)

Controls.AddRange(boxes.OfType(Of Control).ToArray)

If we wanted to inject more functions, we would just add more .WhileEnumerating methods. Make sure however that each of these methods do not have side effects on other methods of the set. There you have it. Go search for those For Each (foreach) loops in your code and see how you can clean them up with LINQ to Objects.

Posted on - Comment
Categories: C# - LINQ - VB - Visual Studio -

LINQ to CSV using DynamicObject and TextFieldParser

In the first post of this series, we parsed our CSV file by simply splitting each line on a comma. While this works for simple files, it becomes problematic when consuming CSV files where individual fields also contains commas. Consider the following sample input:

ALFKI,Alfreds Futterkiste,Maria Anders,"Sales Representative"
ANATR,Ana Trujillo Emparedados y helados,Ana Trujillo,"Owner, Operator"
ANTON,Antonio Moreno Taqueria,Antonio Moreno,"Owner"

Typically when a field in a CSV file includes a comma, the field is quote escaped to designate that the comma is part of the field and not a delimiter. In the previous versions of this parser, we didn’t handle these cases. As a result the following unit test would fail given this sample data:

    Public Sub TestCommaEscaping()
        Dim data = New DynamicCsvEnumerator("C:\temp\Customers.csv")
        Dim query = From c In data
                    Where c.ContactTitle.Contains(",")
                    Select c.ContactTitle

        Assert.AreEqual(1, query.Count)
        Assert.AreEqual("Owner, Operator", query.First)
    End Sub

We could add code to handle the various escaping scenarios here. However, as Jonathan pointed out in his comment to my first post there are already methods that can do CSV parsing in the .Net framework. One of the most flexible ones is the TextFieldParser in the Microsoft.VisualBasic.FileIO namespace. If you code in C# instead of VB, you can simply add a reference to this namespace and access the power from your language of choice.

Retrofiting our existing implementation to use the TextFieldParser is fairly simple. We begin by changing the _FileStream object to being a TextFieldParser rather than a FileStream. We keep this as a class level field in order to stream through our data as we iterate over the rows.

In the GetEnumerator we then instantiate our TextFieldParser and set the delimiter information. Once that is configured, we get the array of header field names by calling the ReadFields method.

    Public Function GetEnumerator() As IEnumerator(Of Object) _
        Implements IEnumerable(Of Object).GetEnumerator

        _FileStream = New Microsoft.VisualBasic.FileIO.TextFieldParser(_filename)
        _FileStream.Delimiters = {","}
        _FileStream.HasFieldsEnclosedInQuotes = True
        _FileStream.TextFieldType = FileIO.FieldType.Delimited

        Dim fields = _FileStream.ReadFields
        _FieldNames = New Dictionary(Of String, Integer)
        For i = 0 To fields.Length - 1
            _FieldNames.Add(GetSafeFieldName(fields(i)), i)
        _CurrentRow = New DynamicCsv(_FileStream.ReadFields, _FieldNames)

        Return Me
    End Function

    Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
        Dim line = _FileStream.ReadFields
        If line IsNot Nothing AndAlso line.Length > 0 Then
            _CurrentRow = New DynamicCsv(line, _FieldNames)
            Return True
            Return False
        End If
    End Function

While we are at it, we also change our MoveNext method to call ReadFields to get the parsed string array of the parsed values in the next line. If this is the last line, the array is empty and we return false in the MoveNext to stop the enumeration. We had to make one other change here because in the old version, we passed the full unparsed line in the constructor of the DynamicCsv type and did the parsing there. Since our TextFieldParser will handle that for use, we’ll add an overloaded constructor to our DynamicCsv DynamicObject accepting the pre parsed string array:

Public Class DynamicCsv
    Inherits DynamicObject

    Private _fieldIndex As Dictionary(Of String, Integer)
    Private _RowValues() As String

    Friend Sub New(ByVal values As String(),
                   ByVal fieldIndex As Dictionary(Of String, Integer))
        _RowValues = values
        _fieldIndex = fieldIndex
    End Sub

With these changes, now we can run our starting unit test including the comma in the Contact Title of the second record and it now passes.

If you like this solution, feel free to download the completed Dynamic CSV Enumerator library and kick the tires a bit. There is no warrantee expressed or implied, but please let me know if you find it helpful and any changes you would recommend.

Posted on - Comment
Categories: Dynamic - LINQ - VB - VB Dev Center - Visual Studio -

LINQ to CSV using DynamicObject Part 2

In the last post, I showed how to use DynamicObject to make consuming CSV files easier. In that example, we showed how we can access CSV columns using the standard dot (.) notation that we use on other objects. Using DynamicObject, we can refer to item.CompanyName and item.Contact_Name rather than item(0) and item(1).

While I’m happy about the new syntax, I’m not content with replacing spaces with underscores as that doesn’t agree with the coding guidelines of using Pascal casing for properties. Because we have control on how the accessors work, we can modify the convention. Let’s reconsider the CSV file that we’re working with. Here’s the beginning:

CustomerID,COMPANYNAME,Contact Name,CONTACT_TITLE,Address,City,Region,PostalCode,Country,Phone,Fax
ALFKI,Alfreds Futterkiste,Maria Anders,Sales Representative,Obere Str. 57,Berlin,NULL,12209,Germany,030-0074321,030-0076545
ANATR,Ana Trujillo Emparedados y helados,Ana Trujillo,Owner,Avda. de la Constituci¢n 2222,Mexico D.F.,NULL,5021,Mexico,(5) 555-4729,(5) 555-3745
ANTON,Antonio Moreno Taqueria,Antonio Moreno,Owner,Mataderos  2312,Mexico D.F.,NULL,5023,Mexico,(5) 555-3932,NULL

Notice here that the header row contains values with a mix of mixed case, all upper, words with spaces, and underscores. To standardize this, we could parse the header and force an upper case at the beginning of each word. That would take a fair amount of parsing code. As a fan of case insensitive programming languages, I figured that if we just strip the spaces and underscores and work against the strings in a case insensitive manner, I’d be happy. In the end, we’ll be able to consume the above CSV with the following code:

Dim data = New DynamicCsvEnumerator("C:\temp\Customers.csv")
Dim query = From c In data
            Where c.City = "London"
            Order By c.CompanyName
            Select c.ContactName, c.CompanyName, c.ContactTitle

To make this change, we change how we parse the header row and the binder name when fetching properties. In our DynamicCsvEnumerator, we already isolated the parsing of the header with a GetSafeFieldName method. Previously we simply returned the input value replacing a space with an underscore. Extending this is trivial:

    Function GetSafeFieldName(ByVal input As String) As String
        'Return input.Replace(" ", "_")
        Return input.
            Replace(" ", "").
            Replace("_", "").
    End Function

That's it for setting up the header parsing changes. We don't need to worry about spaces in the incoming property accessor because it's not legal to use spaces in a method name. I'll also assume that the programmer won't use underscores in the method names by convention. Thus, the only change we need to make in our property accessor is to uppercase the incoming field name to handle the case insensitivity feature. Here's the revised TryGetMember implementation.

    Public Overrides Function TryGetMember(ByVal binder As GetMemberBinder,
                                           ByRef result As Object) As Boolean
        Dim fieldName = binder.Name.ToUpperInvariant()
        If _fieldIndex.ContainsKey(fieldName) Then
            result = _RowValues(_fieldIndex(fieldName))
            Return True
        End If
        Return False
    End Function

All we do is force the field name to upper case and then we can look it up in the dictionary of field indexes that we setup last time. Simple yet effective.

Posted on - Comment
Categories: LINQ - VB Dev Center - Visual Studio - Dynamic -

Pin DataTips in Visual Studio 2010

While debugging in Visual Studio 2010, I noticed that the DataTip now has a new feature. At the right hand side of the variable window, there is now a pin icon.


Clicking on this pin icon adds the DataTip to the code window allowing it to float over the existing text.


In addition to allowing you to drill into the variable’s values as you would in the watch, locals, or autos windows, You can also add comments which remain with the pinned DataTip.


When you stop debugging, the DataTip will disappear. However when you debug into this method again, it will re-appear as long as it is pinned. As a bonus, it will persist even after closing and re-opening Visual Studio.

While Visual Studio 2010 definitely has some rough edges, I continue to be amazed by some of the new UX features that the next version will bring.

Posted on - Comment
Categories: Visual Studio - VB Dev Center -

Win 7 with Visual Studio 2010

I’ve been running Windows 7 since the initial public betas (February 2009) and have loved it. One of my favorite features is the ability to drag an application to the left or right side of the monitor and have it snap  to take up half the screen. Even better is using the Windows – Left Arrow and Windows – Right Arrow key combinations to dock the screens without using the mouse. This even works across multiple monitors. It’s a great addition for comparing documents, or in my case when translating code between C# and VB.

By chance, I was playing with Visual Studio Beta 2 tonight when I slipped while dragging a code window. I intended to do a split screen inside of Visual Studio 2010. Instead, I drug the window outside of Visual Studio. To my surprise, the window moved correctly. Pressing my luck, I pressed Windows-Left Arrow and I was amazed that I now had half of the screen with the newly moved code window and the left half with the rest of visual studio.

Here’s a sample screen shot demonstrating the result. In this case, the PostController from this site is in the left half and the ShowPosts view is on the right half.


I loved that Win 7 docking supported separate apps, but had no idea that the docking would work with parts of the same app. I have a feeling I’ll be using this feature often! Have you discovered other Win 7 secrets with Visual Studio, I’d love to hear them.

Posted on - Comment
Categories: Visual Studio -

LINQ to Entity Visualizer

When demonstrating the LINQ tools, I typically start out showing the LINQ to SQL visualizer that’s available with the C# Samples. Today I saw that Raja Venkatesh has released a Visualizer for ObjectQuery<T> (aka. LINQ to Entities). As you do with the other visualizers, you enable this by simply saving the AnySourceEntityQueryVisualizer.dll to your Visualizers directory. )Note: :the download page specifies to copy it to your C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Packages\Debugger\Visualizers. However, Windows 7 blocks saving files there by default. Alternatively, you can copy it to your My Documents\Visual Studio 2008\Visualizers.

Once you install it, put a breakpoint after your query is declared. If you hover over the query variable, you should see the magnifying glass indicating the debugger visualizer:


Clicking on the magnifying glass brings you to a screen that shows the Expression, projected SQL, and Connection properties. If you click “Execute”, you will see the server log and a grid with the results.


If you want to try it out, feel free to download it from the Visual Studio Gallery page for the LINQ to Entity Visualizer. I’ll be adding a linq to this on my LINQ Tool list as well.

Posted on - Comment
Categories: Entity Framework - VS 2008 - Visual Studio -

LINQ to SQL DataLoadOptions.LoadWith and Take

While trying to increase the performance of this site, I found a bug which may drastically slow the performance. By default when navigating to child objects from a parent object, LINQ to SQL lazy loads the children. This is good when you don't know if you want the children.

However, on this site when viewing posts, I ALWAYS display the categories and number of comments. As mentioned in LINQ in Action, you can eager load child records using the context's LoadOptions to set the child to be eagerly loaded with the parent using the following:

Dim dc As New LinqBlogDataContext
Dim options As New DataLoadOptions
options.LoadWith(Function(p As PostItem) p.CategoryPosts)
options.LoadWith(Function(cp As CategoryPost) cp.Category)
options.LoadWith(Function(p As PostItem) p.Comments)
dc.LoadOptions = options

There are a couple issues with the implementation at this point however. First, the LoadWith setting only works for one level of hierarchy. It does not automatically navigate to grandchildren records. In this case, you may need to project into an anonymous type to remove that extra level of the object graph.

The trickier situation comes when trying to do paging over the result sets. When traversing one level, the LoadOptions work fine for standard queries, however as soon as you throw a Take clause in, the LoadWith options are ignored as shown below:

Dim good =  From p In dc.PostItems _
            Order By p.PubDate Descending _
            Select p

Dim bad =   From p In dc.PostItems _
            Order By p.PubDate Descending _
            Take 5
            Select p

In the first case, a single query is sent to the database when navigating to the children. In the second (bad) query, separate statements are sent to the database as we fetch the children. I submitted a bug item on this. The solution here (if you target the 4.0 framework) is to include a Skip(0) clause which will cause the Take to not short circuit the LoadOptions:

Dim fixed = From p In dc.PostItems _
            Order By p.PubDate Descending _
            Skip 0
            Take 5
            Select p

Unfortunately, this trick doesn't work with the current VS 2010 build when targeting 3.5. I suspect that you may need to target 4.0 in order to get Take to play nice with the LoadOptions.

Posted on - Comment
Categories: Visual Studio - LINQ -

Disable Historical Debugger when using MVC with VS 2010 beta1

I've had a bit of down time between contracts recently and have been taking the opportunity to learn some technologies that I haven't had time to get into before. Since I've read so much about it, I thought I would try out ASP.Net MVC. Since it was just for fun, I figured I'd bite the bullet and try it under VS 2010.

I build a small sample following the tutorials at, however when I try to run it in VS 2010, the app crashes on me. There's enough "Magic" going on inside MVC, including the routing engine and dynamic loading of the controllers and views that trying to debug MVC is challenging enough when the IDE behaves. When it doesn't it makes life significantly more problematic. Naturally VS hard crashes rather than breaking in my code to let me figure out what's going wrong.

It turns out there wasn't a problem in my code, but rather an issue with MVC and the Historical Debugger which is turned on by default in VS 2010. To fix the issue, open the Option dialog (under Tools - Options) and locate the Historical Debugger tab. Uncheck the "Enable Historic Debugging" option.

Historical Debugging Option Screen

Joe Cartano of the ASP team assures us that this will be fixed in Beta 2, so hopefully this is only a temporary situation. Maybe next time I'll remember to read the release notes completely before banging my head against the wall.

Posted on - Comment
Categories: Visual Studio - MVC -

LINQ to SQL designer in VS 2010 Beta 1

There is a bug in the upgrade process when converting a LINQ to SQL designer (.dbml) file from VS 2008 to VS 2010. They changed the implementation to hold the layout information in a .dbml.diagram file rather than the .dbml.layout file. Instead of just renaming the existing file, it replaces it with a new one effectively loosing all of the layout customizations you may have made to the design surface.

Luckily there is an easy fix. Just delete newly created .diagram file and then rename the old .dbml.layout file with a .dbml.diagram extension. If you want to be safe, you can rename the first .diagram file rather than deleting it. When you open your design surface now, your original class layout should be back.

I'm told by the product team that this will be fixed in the next release. That's why it's called a Beta release.

Posted on - Comment
Categories: Visual Studio - LINQ -

LINQ to SQL enhancements for 2010

One question that I'm asked repeatedly is, "Is LINQ dead?" The quick answer is, NO. The more appropriate question is "Is LINQ to SQL dead?" That one is a bit trickier, particularly after some of the blog posts from the data programmability team regarding their emphasis moving forward for LINQ to SQL and the Entity Framework.

My take on it is that LINQ to SQL is in a similar situation to Winforms. Both are still supported and have teams dedicated to them, but don't expect much in terms of new features, rather the focus is on bug fixes. The main development focus for new features is placed more on the Entity Framework and similarly WPF. For those who love taking SAT tests, you can think of it as

LINQ to SQL : Entity Framework :: Winforms : WPF

Proof of the point, Damien Guard, one of the people on the LINQ to SQL team, just posted a list of 40+ changes that are coming for LINQ to SQL for .Net 4.0. Looking through the list, most of the items are bug fix items and not feature enhancements. 

Of the list of items, the biggest change I see is specifying the text parameter lengths. Adding this eliminates some of DBA's performance concerns in terms of query plan reuse. This is one area that DBA's have focused on in terms of the performance issue and was an unfortunate oversight in the initial release.

There are a number of larger features that people continue to ask for that are not being included.

  • Support for more complex table to object relationships. This is really the point of the EDM, so I don't see this ever making it into LINQ to SQL.
  • Ability to update a model from database schema changes. Again, the EDMX designer supports this, so I wouldn't hold your breath.
  • No support for SQL 2008's new data types, including the Spatial types, HierarchyId and Filestream. If any of the features were added, I would expect this to be included at some point.

Damien's list should be proof that LINQ to SQL is not dead, but it isn't going to receive significant enhancements that you may want. Check out the 2010 beta and see what changes are there for yourself and give feedback to the teams before it's too late.

Posted on - Comment
Categories: Entity Framework - LINQ - Visual Studio -

PDC Content comes to Atlanta

In case you missed the PDC, don't miss the opportunity to get some of the content for a greatly reduced cost. Microsoft touring the country with their MSDN Developer Conference  and are coming to Atlanta on December 16th at the Westin Peachtree Plaza.

I'll be among the who's who list of local experts presenting the content. For my part, I'll be showing you the language enhancements that are coming for C# 4.0 and VB 10.0. This release is more about parity than ground-breaking advances like we had with LINQ in the last release. There are a number of nice features which should make programming a bit easier.

In addition to the language session, Glen has a full writeup including the speakers and sessions. There's also a number of cool give-aways. Sign-up today for a great event.

Posted on - Comment
Categories: Code Camp - Visual Studio -