Tag Archive for 'C#'

Realizations

I have been my job for about a month now, and I have some observations I would like to share.

Developing using MS-specific languages (and to a lesser extant, tools) sucks. But not for the reasons you think. (Disclaimer: I am a OS X man, and have been for 3 or 4 years now. Prior to that I was a Windows Xp/Linux man.)

It should be noted that, for the duration of the post, at minimum, I will be speaking .NET 3.5/ASP.NET/C#, and I use Visual Studio Pro 2008, and Expressions Studio 2.

Microsoft Visual Studio make developing you application easy. To easy. I can do all kinds of wonderful things so simply that when I run into something that doesn’t work like I expect, or understand it to, it is a bit surprising. (Sorting with GridView for example, is wonderful, if your application read directly from a database, then spits it out on your view. But try sorting the cached data that you really should be viewing, and the nifty automagic sorting goes away.) So off to Google I go. What do I find? Useless forum posts that say something like

All you need to do is..

YourObject.ThisMethod().ThatMethod();

Well that nice, but what the hell are those methods, or (most likely) I am here because those methods don’t work.

I got into the writing the methods to sort the GridView and since I was in a IEnumerable collection that was [Serialized()], I decided I was going to use LinQ to do all the heavy lifting, but I was unsure of the exact syntax I was gong to need to use. So off to Google I go. I discover, through many sources that I can sort the List in place like so..

List YourList = new List();
YourList.OrderBy(YourListItems => YourListItems.Property)//YourListItems is a temporary variable that gets destroyed once the method is done running, it is storage for the individual ListItems as they are being sorted. Property is one of the instance variables in the class itself, that you are using to sort, a la length in string

Like I said supposedly that sorts your list, buy the property you select, in place. It doesn’t. And since there is no documentation along the lines of the Java SE Docs website for C#, finding out why that wasn’t working was fairly harrowing.

Turns out that all you need to do is…

List YourList = new List();
List YourSortedList = null; //Just to make sure the compiler doesn't try to help
YourSortedList = YourList.OrderBy(YourListItems => YourListItems.Property).ToList();

Basically the .OrderBy() method doesn’t return anything. You MUST invoke the .ToList() method, and put the resulting list somewhere if you want use the results.

I am putting this out there for 2 reasons. First Googling for help was not successful, which leads to; Second, now the answer is on the internet for all to find as needed.