Linq to Objects – join or intersect

Thanks to Linq to objects we can have a more sql like coding style in c#. For somebody that has actual sql knowledge, this comes in very handy.

But it seems that several coding methods perform the same task and gives the same result.
So what coding syntax would you prefer?

For example, assume you want to know which items are given in 2 sets?
You could get this list through following code.


[Tags].net, linq[/Tags]

Charts with MVC and jQuery through jqPlot

Asp.Net MVC is a great way to get dynamic data presented inside a web browser and thanks to the great integration of jQuery now in Visual Studio, we have many programming tools right at hand!

To show of the possibilities, I’ll give an example on how to get data from MVC into your client jQuery code. This example will also use jqPlot to draw a dynamical graph from the given data.

First up, you’ll need to download the jqPlot plugin for jQuery from the jqPlot site: !
I have to say, this graph plugin just looks stunning and is easy enough to use with much tweaking options!!
When this is done, create your MVC project inside Visual Studio.

To get jqPlot working in your MVC project, make sure to create a subfolder jqPlot inside your Scripts folder and copy all .js files from the jqPlot download inside this subfolder!
After that, just add following inside your Site.Master page to get the correct reference to your jqPlot plugin.

Small side note, when you need to edit js files inside Visual Studio and you like to have intellisense for jQuery, be sure to add following line on top of each js file!

Next thing you’ll need to do is add a new controller inside the Controllers section. Inside this controller we will have all calls needed to get a new webpage and also for retrieving data. You add a new controller by right clicking on the Controlles folder and selecting Add>Controller! Give it a meaningful name and click OK.( in this example GraphController.cs )

Add following code inside the controller, this will give us the new web page.

Add following code inside the controller, this will present a JSON data stream back to the client. This data stream represents all markers that will be used by the jqPlot graph to draw lines ( in this example. You can also draw bar or pie charts and many other )

The class used here is called GraphData, it’s created inside the Models section and only contains 2 properties.

Now that the code is in place, right click on the HearthRate() method and select Add View. This will create a new view inside the Views section.

Edit this newly added View ( this example Views>Graph>HearthRate.aspx ) and add following code inside the asp:Content tag! This div will be used by the jqPlot, it indicates where the graph needs to be visible.

The other line is a reference to a new js file that we need for processing the JSON data and presenting it correctly to the jqPlot plugin.

Now the only thing left to do is create the js file with all the jQuery code that will process the JSON stream and draw the line chart.

With all this in place, you can start up your website and browse to the http://localhost/Graph/HearthRate site. If everything goes well, you should get following image.


[Tags], MVC, jQuery, jqPlot[/Tags]

iWatchSyncer – iTunes workflow – part II

Well since iExporter is going so well on CodePlex ( thanks LifeHacker 😉 ), I’ve added my latest version of iWatchSyncer also to CodePlex!!

I finally got around the annoying problem in Windows when you Move folders ( the event handling is different then the Copy action ). So everything should be alright now!

A quick overview: iWatchSyncer will monitor a given path for file or folder additions and will automatically add all mp3 files contained in these folders to the iTunes Music library!

[Tags]iWatchSyncer, iTunes[/Tags]

iExporter – iTunes workflow – part II


I’ve been working on my small iTunes playlist exporting program iExporter. Currently I’m at version 1.6 and it seems it’s been getting some good feedback from several users! Because I’ve seen different websites mentioning it and each release I’ve done so far has had 200 downloads each.

In version 1.6 you can again export existing playlists that are available in your iTunes, but now you’ll also be able to export several artists! The program will search for all tracks in your iTunes collection that have the selected artist as Album Artist, this way you’ll get all albums from that artists!

I hope you like it and if you do, you can now also donate through use of the Paypal donate button available in the iExporter.


[Tags]iExporter, iTunes, Paylists, Mp3[/Tags]

Entity Framework – usage hurdles and tips

Well… let me start with a counter to this blog title! I actually like Entity Framework and don’t mind spending time on getting things done with it.
But, I have to be honest, some things just don’t seem to be right when I’m using it.
It could all just be me, but let me get my message out and share my experience. So here goes… my current hurdles with Entity Framework.

First of, some common issues are already listed on other sites. So first go check this list of issues and then read this post on how to tackle some of those items!

Secondly, most of my ‘problems’ are related to the interaction of the Winforms DataGridView control with EF, so maybe not everything is really due to EF itself.

  • Missing metadata
  • This element is not actually true, because EF has all the metadata a programmer needs. But it’s to far hidden, in my opinion!
    Lets give an example… in my program I want to have several managing forms that actually do nothing more than CRUD actions. So the setup of each form is almost the same, the only thing that is different is the Entity Objects being used in the Grid to show the data.
    But due to the nature of the DataGridView and the usage of ObjectQuery as BindingSource, you will get all the columns that are defined in the ObjectQuery. Meaning Key columns and relational mapping columns will appear inside the grid!
    This is something I don’t want to show inside my grid ( this is my setup, so maybe for some people those columns can be relevant ).

    So I wanted to hide these columns and I didn’t want to do this manually in the designer of the DataGridView for each form. I even didn’t want to know about the names of those fields/columns and write something against that. So the only way I could make this generic was by using the EF metadata of the Entity Object. But that’s where I was stuck… how could I get this information quickly?

    Well my solution uses Extension so I don’t have to type much code to get the metadata

    public static EntityType MetaData(this EntityObject entity, ObjectContext entityContext)
    return entityContext.MetadataWorkspace.GetItemCollection(DataSpace.CSpace).GetItem(entity.GetType().FullName);

    What this allows me to do is get the metadata of an entityobject instance like this

    var col = fooObjectEntity.MetaData(DataContainer.GetInstance().DataEntities).KeyMembers;

    One small problem with this solutions is that you have to pass along the ObjectContext.
    Some people solve this by adding something similar to the ObjectContext itself, take a look here ( it are some thoughts on a question Julie Lerman asked ).
    Passing the ObjectContext is needed, because it’s not possible to get it from the EntityObject itself, as talked about here! ( although I haven’t checked out Patrick’s version of a possible solution yet… look here )

  • Setting the bindingsource
  • I don’t know why, but for some reason I can’t just bind the ObjectQuery to the DataGridView.DataSource directly! Ok maybe wrong again, I can do this and the grid will show the data, but when I add a EntityObject to the given ObjectContext and do a .SaveChanges(), the grid will not refresh the data!! ( after the SaveChanges() I reset the DataSource of the grid to the ObjectQuery )
    I don’t have a clue why this is… updating data will work fine, but adding rows/objects won’t!
    The only solution I found, that solves this is to use Object instantiator of the ObjectQuery. In my case the .ToList() method.

    this.categoryBindingSource.DataSource = DataContainer.GetInstance().DataEntities.Category.ToList();

  • Using related data inside the DataGridView
  • Something that annoyed me was the fact that DataGridView didn’t pick up related data fields as columns.
    For example when you have some EntityObject Member that has a relationship to Address, doing following in code will not get the Address fields as columns inside the grid.

    this.memberBindingSource.DataSource = DataContainer.GetInstance().DataEntities.Member.Include("Address").ToList();

    A pity it doesn’t do this… because when I want to get a row back from the grid I still want to have a EntityObject of type Member, so writing some ObjectQuery myself with all the data seems not the good solution.
    For this little problem I asked what Julie Lerman would do. And she pointed out that the way she does it, is by inserting the values manually during the moment that the grid gets populated. Details are in the comments here. So the solution I used was getting hold of the CellFormatting event and pasting the needed values in the correct cell.

    private void gridSchool_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    if (!this.gridSchool.Rows[e.RowIndex].IsNewRow && this.gridSchool.Columns[e.ColumnIndex].Name.Equals("addressStreet", StringComparison.OrdinalIgnoreCase))
    School currentSchool = this.gridSchool.Rows[e.RowIndex].DataBoundItem as School;
    e.Value = currentSchool.Address.addressStreet;

Well that’s it for the moment… like you see most of the things I stumbled upon are also stuff other people noticed, so maybe they will get addressed during v2 of the Entity Framework ?
On the other hand I still do like the EF as a concept and I hope Microsoft will invest much time and effort into it :).

[Tags]Entity Framework, C#[/Tags]

Culture sensitivity in List<string> – how to do comparison

I recently stumbled upon a small c# problem I thought would have been easy to fix… But even now with the .Net framework 3.5, I can’t find a good solution!

So first the details on the problem. When you are using Generics, it’s very easy to construct lists of strings! Example: List<string> foo = null;
And when you want to search for a specific value inside this string list, you would most likely use the .Contains() method… But this has one huge drawback, it will only use the default equality comparer!
Details can be found in the MSDN information of the .Contains() method on List<T>.

In other words, you can’t search for Culture insensitive stuff, nor can you search for Case insensitive stuff!!
To illustrate, in following example the boolean value of contains will be FALSE, because “test” is not the same as “TEST” based on the case sensitivity of the default equality comparer.

List<string> foo = new List<string>();
bool contains = foo.Contains("TEST");

But thanks to the Exists method on List<T>, we are able to write some extra code that will allow us to check against case insensitive strings.

List<string> foo = new List<string>();
bool containsValue = foo.Exists(delegate(string argument)
return argument.Equals("TEST", StringComparison.OrdinalIgnoreCase);

In this last example, after the Exists method, the boolean value of containsValue will be TRUE… This because now we use the StringComparer class with the indication that we are not interested in case sensitivity!

The given problem has already been brought up by “Andrew Webb” as an enhancement request for the .Net framework ( cfr. his comment in this BCL Team blog post for .Net 4.0 ) and it seems the BCL team will take a look at it. ( fingers crossed )

[tags]c#, Generics[/tags]

SelectionChangeCommited event on Combobox not triggered

There seems to be a problem with the .Net Combobox control ! Microsoft is stating it isn’t actually a real problem, but still it is something a programmer should know and if needed take care of.

Let me first explain the problem… If you use the mouse to open a combobox, but then use the arrow keys to navigate in the list and change focus with the tab key, the combobox will select the highlighted value, but will NOT trigger the SelectionChangeCommited event !!
So in other words, even though the programmes subscribes to this event to know when a user has selected a value, the program will not notice it.

According to this post :, Microsoft is aware of this and regards it as standard behaviour. So we are left with the only option to write a workaround ourselves.

I’ve found a good starting solution here :, but I think this can be improved ! Because with this given solution, if a user still would use the mouse to select an item we would get the SelectionChangeCommited event twice.
So the workaround is to suppress the base event and only trigger our own. Here is the code…

[tags]SelectionChangeCommitted, combobox, c#[/tags]

Serialize/Deserialize objects in .Net – don’t forget to initialize

Recently in our project I had a weird exception popping up in C#. We found the problem and fixed it… but the problem was something we didn’t expect ! So I’ll post my findings here so other people can benefit from it.
I must say, I don’t know if this behaviour is anywhere described in MSDN, so could be I missed the information.

Consider this class Foo with only 1 member O1 that is Serializable.

public class Foo
private object _O1;

public object O1
get { return _O1; }
set { _O1 = value; }

public Foo()
O1 = new object();

Now we are going to save an instance of this Foo class to file through Serialization ! ( here done through unittesting with NUnit )

public void SerializeTest()
Foo foo = new Foo();

BinaryFormatter formatter = new BinaryFormatter();
formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
FileStream stream = new FileStream(@"c:\temp\foo.dat", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, foo);

Now we have a binary representation of Foo on file, called foo.dat !
After this, we change the Foo class and add a second member called O2.

public class Foo
private object _O2;

public object O2
get { return _O2; }
set { _O2 = value; }

private object _O1;

public object O1
get { return _O1; }
set { _O1 = value; }

public Foo()
O1 = new object();
O2 = new object();

And now for the big problem… we Deserialize the foo.dat file with this new Foo class code.

public void DeserializeTest()
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(@"c:\temp\foo.dat", FileMode.Open, FileAccess.Read, FileShare.Read);
Foo foo = (Foo)formatter.Deserialize(stream);

And what do you notice ? The test wil FAIL !! Yes… what we didn’t expect was the fact that .Net somehow can initialize the Foo object without going through it’s default constructor !!
Resulting in a Foo object with a O2 member that equals NULL instead of new Object() !

This would mean that if you need to deserialize newer class versions of existing binary objects, you should use the interface ISerializable and use protected Foo(SerializationInfo info, StreamingContext context) to be sure all members ( and new members ) are serialized and initiated ?

Can anyone shed some light on this ?

[Update] Jelle has given me the correct link to the MSDN documentation that gives the answer to this phenomenon !! Thanks Jelle !

[tags]C#, .Net, Visual Studio[/tags]

To comment or not to comment

Damn, Chad Myer‘s post about mistakes of comment usage in code made me think !

I tend to agree with him about the fact that your own code should be self explanatory, so that code comment is redundant.
But on the other hand, sometimes a little comment about the flow of the code always tends to help me understand the code itself better.
That aside, my code doesn’t have that much of code comment ( except for the //TODO: lines ) !
So I had to search a bit, but found a good example IMO, to show of the need of code comment… This to answer Chad’s question !

So here it is, what do you think ? Is the comment valid, should it stay in the code ? I think it should ! It still seems very obvious, but I would like to keep it in.

/// The general list has 2 internal lists:
/// * one pointing to the actual objects in the database
/// * one pointing to the actual objects in memory
/// To get the total count we have to take following flow into account
/// * the memory reference can be different from the database reference, so we combine the 2 counts and subtract the objects that are already available in the database list
public int Count
int totalCount = 0;

totalCount = ReferenceEquals(this.internalListMemory, null) ? 0 : this.internalListMemory.Count;
totalCount += ReferenceEquals(this.internalListDatabase, null) ? 0 : this.internalListDatabase.listCount;
foreach (DictionaryEntry internalListDataObjectsElement in this.internalListMemory)
if (!ReferenceEquals(this.internalListDatabase, null) && this.internalListDatabase.ListFind(internalListDataObjectsElement.Key))
totalCount -= 1;

return totalCount;

Like I stated above, maybe the code comment is a bit to abvious ( so it could be omitted, but I’m in favour to let it in ).
So here maybe another example… but also here a small suggestion, this could maybe be changed to a //TODO: version. Depending on the work flow of programmer and his Task list.

/// There is a bug with the Excel interop for Office XP ! We first need to set the current culture to en-US to get correct date time values
public void StartTimeProcessing()
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");

- Rest of code with use of the interop code

Default user Locale through .Net

kick it on

For who doesn’t know this yet, I’m a software programmer !
Currently I’m working with Visual Studio 2005 and develop software with C#.

And every now and then you get stuck in a situation that seems hopeless :). Even with a large team of capable people and the vast code example base available on the internet, some problems seem unsolvable !
So let me tell you a small story about one of this problems I recently had.

First the current layout of the project, we create software, but we actually sell a complete system to our customers. This means that we will ship a Dell desktop model together the software ! Then our service people will run the install scripts on site and configure it to the needs of the customer. But during the configuration, the service guy has to create several users through the configuration program. These users are stored in Windows XP as actual users, so when the service guys creates a new user, the default settings for this user are coming from Windows XP itself.
More technical detail : there is a .Default user defined in the Windows Registry that acts as a template for each new user !

Now the problem with this setting… Each time the service guy adds a user he has to set this user to the correct Locale, depending on the country where the software is going to get used ! This because the user account for the service people is put to English – US and it could be that each user has to be set to Dutch – Belgium.

To facilitate the install, we had the idea of manipulating Windows XP in such a manner that during configuration of our software the service guy can pick one Locale that will have the effect that each time he adds a new user it will already have those Locale settings.

For this problem I was asked to come up with a good solution in C# !!
And well to cut a long story short, I haven’t found one !!!!! So I was stuck.
The only real solution I found had to many pitfalls, we regarded it as a ‘no go’. It consist of creating a CultureInfo object from a given LCID and dumping all data into the Registry node of HKEY_USERS\.Default\Control Panel\International ! Even though I think this should/could work, I think that string manipulating each value to the correct format would lead to many possible errors.

What happens when I’m stuck… I go around the internet and search for topics that are closely linked. Well let me say that since the dawn of Blogs, we are blessed with information and knowledge that has impact on real life development !!
As a blogger myself, I know that writing a good article with some tutorial knowledge so since developers started using this medium my life got a lot easier :).
Because of my current working position I follow a lot of MSDN blogs that have a close or near relationship with .Net, Visual Studio or C#. One of those blogs is the one of Shawn Steele, and let me tell you his blog is superb ! Easy to follow and the example code/solutions he posts are all eye openers for me.

Well because he had already written many posts about cultural settings, I tried to contact him to see if he could shed some light on my problem. And guess what, I got a solution within 24 hours !!!!! That isn’t even damn fast, that is just amazingly brilliant !! His original answer is situated here.
Thanks again Shawn !!

After looking at the solution, there are 2 lesson to be made :

1. Not everything can be done with .Net ( so hence the reason why I got stuck 🙂 )
2. Not all solutions need to be difficult ( just one Windows script command line does exactly what we wanted )

Now for all other people out there who would stumble upon this same problem, here is the code I used in .Net to launch the script, enjoy.

public void StoreCultureInfo(int lcid)
//First create a CultureInfo object from the given LCID code
CultureInfo cultureInfo = CultureInfo.GetCultureInfo(lcid);

//Setting the .Default user in the Registry can't be done through .Net code in a straightforward manner !
//But there is a way to do this through use of command line syntax, the details can be found here :;en-us;289125
//Reminder : This option will only work for Windows XP, in Windows Vista detailed solution can be found here :

//First we need to create a text file with the correct Locale for the Default user
//The locale should be specified in Hexadecimal format !
string hexadecimalLCID = cultureInfo.LCID.ToString("X");
string localeConfigurationFile = Path.Combine(PathHelper.TempDirectory, "regopts.txt");
//Be sure this configuration file doesn't exist yet
if (File.Exists(localeConfigurationFile))

using (StreamWriter streamWriter = File.CreateText(localeConfigurationFile))
streamWriter.WriteLine("UserLocale_DefaultUser = {0}", hexadecimalLCID);

//Now execute the command line option to swith the default user locale !
Process process = new Process();
process.StartInfo.FileName = "rundll32.exe";
process.StartInfo.Arguments = string.Concat("shell32,Control_RunDLL intl.cpl,,/f:", "\"", localeConfigurationFile, "\"");
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

int processExitCode = process.ExitCode;

if (processExitCode != 0)
throw new ApplicationException("Problem occured while setting the Default user locale !");
catch (ApplicationException ex)

Technorati tags: , , , ,