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: