Tuesday, November 27, 2007

LINQ to Command Line Arguments

Jay Kimble - Using LINQ to Query the Command Line args

"...

Today, I just started thinking about this code... I really don't like it.  It doesn't really evoke what I want to say.  I want my code to say "does such and such argument exist in the string array."
Using LINQ we get a cleaner (in my opinion) variation of the above and it "says" what I want it to "say."

   1: // Class/Namespace deleted for brevity
   2: static void Main(string[] args)
   3: {
   4:     bool AAArgumentExists = (from arg in args
   5:                                   where arg == "-aa"
   6:                                   select arg != "").FirstOrDefault();
   7: }

Of course something else I tend to do is have an argument for passing in a  date value (just something I seem to run into on a regular basis).  Here's the code snippet to do that (I wish it were a little more terse... but I'm still a newbie to LINQ).

..."


That's just too cool. I think it's LINQ's ability to query objects/collections/arrays/etc is what is going to draw me into .Net 3.5...


Just thinking about all the time/code spent on looping through collections looking for stuff (or sorting, filtering, etc, etc) makes me shudder...


LINQ to SQL for me is a wait and see, but LINQ to Objects? That's quickly becoming must have...

No comments:

Post a Comment

NOTE: Anonymous Commenting has been turned off for a while... The comment spammers are just killing me...

ALL comments are moderated. I will review every comment before it will appear on the blog.

Your comment WILL NOT APPEAR UNTIL I approve it. This may take some hours...

I reserve, and will use, the right to not approve ANY comment for ANY reason. I will not usually, but if it's off topic, spam (or even close to spam-like), inflammatory, mean, etc, etc, well... then...

Please see my comment policy for more information if you are interested.

Thanks,
Greg

PS. I am proactively moderating comments. Your comment WILL NOT APPEAR UNTIL I approve it. This may take some hours...