Sunday, 12 February 2012

Introduction to the LINQ

Introduction to the LINQ


Introduction to Querying


When using for or foreach loops on a list, you get the value or a range of values inside of the loop. Once you exit the loop, the operation ends and you cannot access the value(s) that was(were) isolated. If you want to get the isolated value or a list of values again, you would have to perform the operation (create the loop), again.
In some cases, you may want to prepare and get one value, a few values, or a range of values for later use, or to use over and over again. To do this, you would create a value or a list of values and store that list in a variable, outside of any loop, then use the value or the list of values when needed. As applied to the for or the foreach loop, to perform this operation, you use a conditional statement that would examine the list, look for the value(s), get that value or those values that respond(s) to the condition. Any value(s) that respond(s) to the condition is(are) then stored in the new list. This technique of examining a list is referred to as querying.
Practical Learning: Introducing LINQ

  1. Start Microsoft Visual Studio and create a Console Application named AltairRealtors1
  2. To create a new class, in the Class View, right-click AltairRealtors1 -> Add -> Class...
  3. Change the name to Property and press Enter
  4. Complete the class as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace AltairRealtors1
    {
        public enum PropertyType
        {
            Condominium,
            Townhouse,
            SingleFamily,
            Unknown
        }
    
        public enum PropertyCondition
        {
            Unknown,
            Excellent,
            Good,
            NeedsRepair,
            BadShape
        }
    
        [Serializable]
        public class Property
        {
            private int nbr;
            private PropertyType tp;
            private string ct;
            private string stt;
            private PropertyCondition cond;
            private short beds;
            private float baths;
            private int levels;
            private int yr;
            private decimal val;
    
            public Property()
            {
                nbr = 0;
                tp = PropertyType.Unknown;
                ct = "Unknown";
                stt = "AA";
                cond = PropertyCondition.Unknown;
                beds = 0;
                baths = 0.00F;
                levels = 0;
                yr = 1900;
                val = 0M;
            }
    
            public Property(int propNbr, PropertyType type, string city,
                            string state, PropertyCondition condition,
                            short bedrooms, float bathrooms, int stories,
                            int year, decimal value)
            {
                nbr = propNbr;
                tp = type;
                ct = city;
                stt = state;
                cond = condition;
                beds = bedrooms;
                baths = bathrooms;
                levels = stories;
                yr = year;
                val = value;
            }
    
            public int PropertyNumber
            {
                get { return nbr; }
                set { nbr = value; }
            }
    
            public PropertyType Type
            {
                get { return tp; }
                set { tp = value; }
            }
    
            public string City
            {
                get { return ct; }
                set { ct = value; }
            }
    
            public string State
            {
                get { return stt; }
                set { stt = value; }
            }
    
            public PropertyCondition Condition
            {
                get { return cond; }
                set { cond = value; }
            }
    
            public short Bedrooms
            {
                get
                {
                    if (beds <= 1)
                        return 1;
                    else
                        return beds;
                }
                set { beds = value; }
            }
    
            public float Bathrooms
            {
                get { return (baths <= 0) ? 0.00f : baths; }
                set { baths = value; }
            }
    
            public int Stories
            {
                get { return levels; }
                set { levels = value; }
            }
    
            public int YearBuilt
            {
                get { return yr; }
                set { yr = value; }
            }
    
            public decimal MarketValue
            {
                get { return (val <= 0) ? 0.00M : val; }
                set { val = value; }
            }
        }
    }
  5. In the Solution Explorer, right-click Program.cs and click Rename
  6. Type AltairRealtors.cs and press Enter twice to display the file
  7. Change the file as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace AltairRealtors1
    {
        public class AltairRealtors
        {
            public static int Main(string[] args)
            {
                Property[] lstProperties = new Property[]
                {
                    new Property(524880, PropertyType.SingleFamily, "Silver Spring", "MD",
                            PropertyCondition.Good, 4, 2.50f, 3, 1995, 495880.00M),
                    new Property(688364, PropertyType.SingleFamily, "Alexandria", "VA",
                            PropertyCondition.Excellent, 4, 3.5f, 2, 2000, 620724.00M),
                    new Property(611464, PropertyType.SingleFamily, "Laurel", "MD",
                            PropertyCondition.Good, 0, 0F, 2, 0, 422625.00M),
                    new Property(749562, PropertyType.Townhouse, "Gettysburg", "WV",
                            PropertyCondition.Good, 3, 2.5F, 3, 2002, 425400.00M),
                    new Property(420115, PropertyType.Unknown, "Washington", "DC",
                            PropertyCondition.Unknown, 2, 0F, 0, 1982, 312555.00M),
                    new Property(200417, PropertyType.Condominium, "Germantown", "MD",
                            PropertyCondition.Excellent, 2, 1f, 0, 0, 215495.00M),
                    new Property(927474, PropertyType.Townhouse, "Arlington", "VA",
                            PropertyCondition.BadShape, 4, 2.5f, 3, 1992, 415665.00M),
                    new Property(682630, PropertyType.SingleFamily, "Martinsburg", "WV",
                            PropertyCondition.Good, 4, 3.5f, 3, 2005, 325000.00M),
                    new Property(288540, PropertyType.Condominium, "Silver Spring", "MD",
                            PropertyCondition.Good, 1, 1f, 0, 2000, 242775.00M),
                    new Property(247472, PropertyType.SingleFamily, "Silver Spring", "MD",
                            PropertyCondition.Excellent, 3, 3f, 3, 1996, 625450.00M),
                    new Property(297446, PropertyType.Townhouse, "Laurel", "MD",
                            PropertyCondition.Unknown, 4, 1.5F, 2, 2002, 412885.00M),
                    new Property(924792, PropertyType.SingleFamily, "Washington", "DC",
                            PropertyCondition.Good, 5, 3.5F, 3, 2000, 555885.00M),
                    new Property(294796, PropertyType.SingleFamily, "Falls Church", "VA",
                            PropertyCondition.Excellent, 5, 2.5f, 2, 1995, 485995.00M),
                    new Property(811155, PropertyType.Condominium, "Alexandria", "VA",
                            PropertyCondition.Good, 1, 1.0F, 0, 2000, 352775.00M),
                    new Property(447597, PropertyType.Townhouse, "Hyattsville", "MD",
                            PropertyCondition.Excellent, 3, 2f, 3, 1992, 365880.00M),
                    new Property(297415, PropertyType.Townhouse, "ashington", "DC",
                            PropertyCondition.Good, 4, 3.5f, 1, 2004, 735475.00M),
                    new Property(475974, PropertyType.SingleFamily, "Gaithersburg", "MD",
                            PropertyCondition.Unknown, 4, 2.5f, 1, 1965, 615775.00M),
                    new Property(927409, PropertyType.Condominium, "McLean", "VA",
                            PropertyCondition.Excellent, 1, 1f, 12, 2006, 485900.00M),
                    new Property(304750, PropertyType.Condominium, "Washington", "DC",
                            PropertyCondition.Unknown, 2, 2f, 6, 1992, 388665.00M),
                    new Property(207850, PropertyType.Townhouse, "Rockville", "MD",
                            PropertyCondition.Good, 3, 2.5F, 2, 1988, 525995.00M)
                };
    
                FileStream stmProperties = null;
                BinaryFormatter bfmProperties = new BinaryFormatter();
    
                // If this directory doesn't exist, create it
                Directory.CreateDirectory(@"C:\Altair Realtors");
                // This is the file that holds the list of properties
                string Filename = @"C:\Altair Realtors\Properties.atr";
    
                // Find out if there is already a file that contains a list of properties.
                // If that file exists, open it to get it ready for the new properties.
                if (File.Exists(Filename))
                {
                    stmProperties = new FileStream(Filename,
                                                  FileMode.Open,
                                                  FileAccess.Read,
                                                  FileShare.Read);
    
                    try
                    {
                        // Retrieve the list of items from file
                        lstProperties =
                            (Property[])bfmProperties.Deserialize(stmProperties);
                    }
                    finally
                    {
                        stmProperties.Close();
                    }
                }
    
                // Save the list of properties
                stmProperties = new FileStream(Filename,
                                              FileMode.Create,
                                              FileAccess.Write,
                                              FileShare.Write);
    
                try
                {
                    bfmProperties.Serialize(stmProperties, lstProperties);
                }
                finally
                {
                    stmProperties.Close();
                }
    
                return 0;
            }
        }
    }
  8. Execute the application to create the list
  9. Close the DOS window and return to your programming environment
  10.  
  11. Change the file as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace AltairRealtors1
    {
        public class AltairRealtors
        {
            Property[] lstProperties;
    
            public static int Main(string[] args)
            {
                FileStream stmProperties = null;
                AltairRealtors ar = new AltairRealtors();
                BinaryFormatter bfmProperties = new BinaryFormatter();
    
                // This is the file that holds the list of properties
                string Filename = @"C:\Altair Realtors\Properties.atr";
    
                // Find out if there is already a file that contains a list of properties.
                // If that file exists, open it.
                if (File.Exists(Filename))
                {
                    stmProperties = new FileStream(Filename,
                                                  FileMode.Open,
                                                  FileAccess.Read,
                                                  FileShare.Read);
    
                    try
                    {
                        // Retrieve the list of items from file
                        ar.lstProperties = (Property[])bfmProperties.Deserialize(stmProperties);
                    }
                    finally
                    {
                        stmProperties.Close();
                    }
                }
    
                return 0;
            }
        }
    }
Introduction to LINQ


To support the ability to query a list, you can use the Language Integrated Query, abbreviated LINQ. To use LINQ in your application, you must include the System.Core.dll assembly in your program. If you started your application as an empty project:
  • On the main menu, you can click Project -> Add Reference...
  • In the Solution Explorer, you can right-click the name of the project and click Add Reference...
  • In the Class View, you can right-click the name of the project and click Add Reference...
In the .NET tab of the Add Reference dialog box, you can click System.Core

Add Reference
Then click OK. You must then use the System.Linq namespace in your code or you can include the using System.Linq; line in your list of namespaces.
Creating a Query

To query a list, you write a statement using words and operators of the LINQ. The most fundamental operation you can perform on LINQ consists of creating, also referred to as selecting, or querying, a list of values, from an existing list such as an array. The basic formula to use is:
var SubListName = from ValueHolder in List select ValueHolder;
The var keyword, the assignment operator "=", the from keyword, the in keyword, the select keyword, and the semicolon are required.
The SubListName is a name of a new variable that will hold the list of values produced by this operation.
The ValueHolder is the name of a variable that will be used to identify each resulting member of this operation. This variable will be equivalent to getting each member of the list and that responds to a condition.
The List factor represents the name of the variable that you would have created already. The List can be an array. Here is an example:
using System;
using System.Linq;

public class Exercise
{
    public static int Main()
    {
        var numbers = new double[] { 12.44, 525.38, 6.28, 2448.32, 632.04 };

        var number = from n in numbers select n;

        foreach (var member in number)
            Console.WriteLine("Member: {0}", member.ToString());
        return 0;
    }
}

This would produce:
Numbers

To make the code easier to read, you can spread the select statement to various lines. Here is an example:
var number = from n
      in numbers
      select n;

No comments :