Enumerable.Range Pattern in C#

by mheydt 30. October 2009 14:25 >
I'm a big fan of Linq to objects.  I really like the way that you can write much more concise code with it for every day things, not just things that you would think that you normally do with Linq.

One of the things that I almost always do now is use the Linq ForEach instead of the C# foreach statement.  This gives you a nominal amount of code compression.  But the thing I want to write about is using Linq instead of the C# 'for' statement, which I think leads to a lot nicer code. 

I came across this the other day as I actually needed to write a 'for' statement to add a specific amount of nodes to a graph.  Upon writing it, I was like 'gee, there must be a more concise way of doing this', such as where I don't need to do 'for', an initializer, comparison, and incrementation sub-statements.  I mean, I just need to iterate across the integers 0 to 10 and apply a function to each.

The basic model that think of for doing this is provided in Ruby with sequences / ranges.  As an example, here is some ruby that generates the integers 1 to 10 and prints each to the console:



I'm not going to explain ruby in detail, but the (1..10) generates the integers 1 to 10, the '.each' says for each item in the sequence apply the code block that follows.  The code block is essentially a delegage with 'i' as a parameter and which prints 'i' to the console.

So how can this be done in C#?  Well, it requires a couple of steps.  First, System.Linq provides extensions to the Enumerable class, notably the 'Range' method which generates the values between two parameters:



This works fine, but I have an issue with it.  Enumerable.Range (and most things in Linq) returns an IEnumerable.  To iterate across those, you need to use the foreach statement (or a for, or while, that uses the IEnumerable methods), which is not really better than having to use the for loop.  What I'd like is something still more concise.

One thing that can be done is to convert the enumerable to a list using the ToList() Linq extension method:



This works great, but I really don't like having to convert the result of .Range() to a list just so I can apply the .ForEach() method to the result.  Why the Linq extensions don't have a .ForEach() on IEnumerable I don't know; it sure would be handy.  But, we can write our own:



Now that we have this, we can rewrite the code to the following, which is what I've been looking for the whole time:





Perfect, concise, and also using a fluent interface that I really like.


Tags:

C# | LInQ

Coverting CSV to String Matrix with LINQ

by mheydt 10. June 2009 13:34 >
I came across the need today to convert some CSV (excel data on the clipboard) into an array of arrays strings (string[][]); basically a matrix of strings where each matrix element is one of the cells in Excel.

Usually I'd do this through conventional means like nested foreach statements, but I decided I'd like to try this with LINQ.  This is the result I got, which I thought was so very nicely concise:


Tags:

C# | LInQ

EC2 WPF/Silverlight Library

by mheydt 14. January 2009 01:08 >
I've started the development of this library.  It's been an interesting experience using the existing Amazon EC2 C# library as a base for this library.  All I've got to say about that library is it really is coded weird;  I'm not going to say its coded badly, lets just leave it at weird.  Lets just say it really appears to be coded by a non .net person.

If you remember from an earlier post, I have a few requirements for this library:
  • It should compile and run under both WPF and Silverlight with the same code base
  • It should make asynchronous calls the norm (since that is the model for Silverlight)
  • It should use the WebClient class, the norm for Silverlight

Yes, I'm kind of coding to the least common denominator of Silverlight (as versus to WPF), but it is a better model in my mind anyway.

One of the first issues I had was in determining how to issue REST requests for EC2 using the WebClient class.  Believe it or not, I really did not find a lot of info on this while googling around.  In the end, it turned out to be pretty easy.  The code to do this is exemplified in this picture:


In short, this method takes a dictionary of key value pairs which are mapped to REST parameters.  Each caller of this method (like the AttachVolume method) will set its parameters (at a minimum the Action key).  It then adds in the required parameters in the call (access keys, signatures, ...), most of which are passed in on the constructor of this object and others calculated on the fly.

The WebClient object is then created, and the proxy added if in use, and then two headers that are required.  A callback is setup for handling the async response, and then the data is passed up with a POST method using the UploadDataAsync method.  I happen to pass a custom object as the UserData that encapsulates a callback passed from the calling method, and that can be seen in the explanations to come as well as by examining the async handler.

That's pretty much it to send the REST command to EC2.  Now lets see how to construct a command that uses this technique.

I've been building this out with an immediate need which requires me to be able to attach and detach volumes from an EC2 instance, so I have focused on the Attach-Volume, Detach-Volume, as well as the Describe-Volumes commands (the last is needed to track the attach/detach status).  As an example, lets look at the code to detach a volume:



The method first sets up the parameters required for this actual process, and then calls the invoke method to send the command to EC2.  You can see here that I also pass a delegate to a callback that will handle the response (the invokes call back calls this method), and I also allow higher layers to pass their own user data (it helps this library to deal with multithreading).

The real trick now comes in the handling of the response in the callback, and where I think my approach differs greatly from the existing Amazon EC2 library.  That library applies XSLT embedded in the resource to build a result object (and also does not do things async, as well as having all kinds of other overhead), where I just use a little Linq to XML to process the return XML into an actual object.  Did I say I really like Linq to XML?

Matter of fact, let me show the code for the describe volumes command.  This is nead as it has a link query that shows two levels of query in one statement, as the response is an array of volumes, with each volume having a potential of multiple attachement objects.  The Linq to XML below does this easily and clarly in one statement:




Notice the nested from statement;  Attachments is a list property of the DescribeVolumesResult class, and the "from" will be interpreted to be the currently selected volume entry in the XML as represented by the volume variable.  I also really like the use of absolute XPath to select the outer level objects, and then the relative XPath that is automatically relative to the XElement selected from the outer "from".

BTW, did I say I love Linq to XML?

I have a few more layers on top of this, but this is the basic stuff to make this work.  Those upper layers allow things like blocking until a detach/attach is complete, which I need for scenarios that I will also explain at another time.  There is also some interesting things that I will have to say about multi-targeting this code for both WPF and Silverlight that I'll discuss at another time (but for now lets say its a pain in the arse).

Tags:

.Net | AWS | EC2 | LInQ

C# 3.0 Features Added for LInQ

by mheydt 15. July 2008 17:45 >
I'm brushing up on my LInQ today for a few reasons.  One is that its been a while since I used it because my current client isn't that far ahead and I need a refresher.  Second is that I want to build up some interview questions.  Third is I need to update an intro to LInQ presentation I have previously built. 

The true reason though is that I want to write out a few of my opinions on the technology, and explain a few things from the perspective of how I see them.  One of those things is how the new C# 3.0 language extensions facilitate LInQ.  I think sometimes that people think that these features were added to C# as independent items that developers have requested.  I don't really see it that way, as I think that although they are useful (and very useful at that) by themselves, that they are only added to the language to allow LInQ.  If they were not added, the expressivness of LInQ statements would be greatly reduced, and as a matter of fact it would not event be possible.

The following are the new C# 3.0 features and how I see them related to LInQ:
  • Local Type Inference
Required for handling query / selection results.  Often (mostly) the type of this object may not be of a type that is defined in the system at compile time.  An example is a projection of just a few properties from a result object (via select) instead of the whole object.  In this case, the compiler / run-time will create an anonymous type whose type (likely a class with with properties) that can be inferred through the a var keyword.  This is tightly intertwined with anonymous types.
  • Anonymous Types
Required to allow the creation of results from where and select queries.
  • Lambda Expressions
Required to efficiently code expressions for the selection and projection steps of a query.  With out them, you would need to declare a delegate (either explicitly or anonymously) which would add to the coding overhead.
  • Extension Methods
Needed to extend existing classes to have new LInQ methods such as .Where, .OrderBy, .Select, ...
  • Object Initializers
Allow the compiler / run-time to efficiently initialize properties of anonymous classes.  Without them a constructor would need to be injected into the anonymous class.
  • Query Expressions
Provide a syntax similar to SQL, which is used to manipulate data, but provides a short-hand for the coder that the compiler converts into specific classes and method in LInQ.

Tags:

.Net | C# | LInQ

about the author

I'm a .NET, XAML, and iOS polyglot that loves playing with new things and making cool and innovative stuff.  I am also a Mac junkie.

I am Principal Technologist for SunGard Global Services in NYC, in their Advanced Technologies practice, and I work extensively with SunGard's energy and financial customers.

Note the the posting on this blog are my own and do not represent the position, strategies or opinions of SGS.

twitter

I can't stop thinking big!
Sunday 1:08AM via WindowsLive
Just watched Moneyball. That's my pick for best movie this year.
Saturday 3:51PM via WindowsLive
@vincebelpiede: Report: Skype For Windows Phone Beta Imminent http://t.co/KYNjgg1L#mhtnd
Wednesday 8:39AM via Twitter for Mac
@mashable: Kinect Fusion Will Turn Gaming (and More) Into a 3D Fun House - http://t.co/Ihrq2fY2#mhtnd
Wednesday 8:39AM via Twitter for Mac
New Kinect SDK: http://t.co/57MvA5L5 #mhtnd
Wednesday 8:39AM via Twitter for Mac
Follow me on Twitter

recent comments

None

month list