Wednesday, April 20, 2011

Gameutopia!

Mortal Kombat & Portal 2 quite possibly one of the good combo games to play concurrently :)

Going to take my time playing these two unlike crysis 2 which i beat it in 2 days and got bored out of my mind since then.

One eye for Fatality and One eye for Science!!

PS: Next post is on my first application on Windows Phone 7...

Friday, April 1, 2011

LINQing - The Basics

Been a while since I made my promise, nevertheless despite me wanting to post something interesting. I would very much like to stick to my promise, so here it goes.

LINQ stands for Language Integrated Query is a set of features listed from Visual Studio 2008 (.Net 3.5 and above) it extends query capabilities to Language Syntax of C#. I tried explaining the architecture of LINQ to people I know at work they seemed least bit interested and judging from their experience they wanted to see it in action directly rather than wanting to know about the entire architecture so I thought of adapting the same format for this post. It will be direct hands on and anyone interested in learning the architecture can find more accurate information here

The gist of this post is to explain the concept of LINQ to XML and this will be accomplished firstly by the creation of a XML Document using LINQ to XML features followed by a simple, update and delete query. (I wanted to go into more complex Lambda expressions but most people don't like reading for a long time so another time may be ;))

First I shall start with the creation of a XML Document. The code below is written in C# (.Net 3.5) and it's Visual Studio 2008 compilable.

//Creating XML USing LINQ Statement

 XElement xelem = new XElement("GameIcons",
                      new XElement("Icon1",
                          new XElement("Name", "Gordon Freeman"),
                          new XElement("Game", "Half Life"),
                          new XElement("PrimaryWeapon", "Crowbar"),
                          new XElement("SecondaryWeapon", "GravityGun")),
                      new XElement("Icon2",
                          new XElement("Name", "Master Chief"),
                          new XElement("Game", "Halo"),
                          new XElement("PrimaryWeapon", "Assault Rifle"),
                          new XElement("SecondaryWeapon", "Melee")),
                      new XElement("Icon3",
                          new XElement("Name", "Ezio"),
                          new XElement("Game", "Assassin's creed"),
                          new XElement("PrimaryWeapon", "Hidden Blade"),
                          new XElement("SecondaryWeapon", "Sword"),
                          new XElement("Misc","Throwing Knives")));

             xelem.Save(@"C:\LINQTutorial\LINQTutorial.xml");

Can it get any simpler than this, The document is created using XElement (equivalent to the old XmlNode) and the biggest factor is the simplicity and readability it adds to the language and the same hierarchical structure you realize with the XML file structure can be realized here. Anyone having difficulty in following the steps can be clarified easily once they see the file which has been created.


  
    Gordon Freeman
    Half Life
    Crowbar
    GravityGun
  
  
    Master Chief
    Halo
    Assault Rifle
    Melee
  
  
    Ezio
    Assassin's creed
    Hidden Blade
    Sword
    Throwing Knives
  

XML with customer names and phone numbers are so boring don't you think? I Wonder if anyone ever came up with a XML file like this before, seems like Gordon Freeman (MIT Graduate as he  is) is wiling to be the first test subject. I would like to show case a simple select query to show the basics.(this should be very easy test to him than the teleportation test he takes in Half Life 2)

String sXmlValue = File.ReadAllText(@"C:\LINQTutorial\LINQTutorial.xml");
XDocument xdoc = XDocument.Parse(sXmlValue);
//Sample Select Statement
            
var nameList = from obj in xdoc.Descendants("Icon1")
               select new
               {
                name=(string) obj.Element("Name"),
                game=(string) obj.Element("Game"),
                primaryWeapon=(string) obj.Element("PrimaryWeapon"),

                };

            foreach (var varName in nameList)
            MessageBox.Show(varName.name + "\t" + varName.game + "\t" + varName.primaryWeapon);
      
The functionality is very simple, I read the contents of the XML file created previously and using the XDocument to parse the contents of the string (XDocument is equivalent to XmlDocument in terms of functionality). Now onto the query component which is the backbone the query returns the list of variable elements for the XML file with child node as Icon1 and from those I pick the individual child name elements directly based on their String value. This will display the contents of the Icon1 that is details about Gordon Freeman as formatted by MessageBox Contents(Yes including the gravity gun). I am sure the readability aspect is very lucid for this task.

Seems Master Chief wants to go next for the Update Trial. He isn't satisfied with Melee as his Secondary Weapon and he needs a Rocket Launcher. So the next LINQ Statement will fix that aspect for him.

//Simple Update Statement

IEnumerable xval = (from obj in xdoc.Descendants("Icon2")
                                         select obj).ToList();

//Giving Rocket Launcher as Secondary Weapon to Master chief
foreach (XElement xe in xval)
    xe.Element("SecondaryWeapon").SetValue("RocketLauncher");


Cool. Huh? Master Chief is very happy with his Rocket Launcher and finally Ezio seems to wonder why he has the Misc child node when Freeman and Chief doesn't have it, so I will comply to the wish of Ezio and will remove the node for him. Delete statement is even more straightforward than the update statement.

//Delete Statement
IEnumerable xval1 = (from obj in xdoc.Descendants("Icon3")
                                          select obj).ToList();

foreach (XElement xe in xval1)
      xe.Element("Misc").Remove();

Ezio takes the leap of faith gladly now that his wish is granted. It's very hard to cover the abstraction and underlying concepts of LINQ in a single post. I did my post to uncover the most basic elements and once the basic concepts strike home more complex lambda expressions are very easy to learn. Always the best place to start would the MSDN Documentation.

Once the Intricacies are uncovered. LINQ is in fact Child's play! (now you know the allusion to the gaming references)

I really enjoyed writing this post. Until Next time!