Home

Introduction to the LINQ

   

The Language Integrated Query

 

Introduction

 

The Language Integrated Query, abbreviated LINQ, is a technology used to create a sub-list based on an existing list. The new list could be the same as the original, it could be a list of selected values from the original, or it can be a different arrangement of the values of the first list.

   

Practical Learning: Introducing LINQ

  1. Start Microsoft Visual Studio and create a Windows Forms 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 Form1.cs and click Rename
  6. Type AltairRealtors.cs and press Enter twice to display the form
  7. Design the form as follows:
     
    Altair Realtors
     
    Control Text Name Other Properties
    ListView List View   lvwProperties Anchor: Top, Bottom, Left, Right
    Columns  
    (Name) Text TextAlign Width
    colIndex #   40
    colPropertyNumber Prop #   55
    colDateListed Date Listed Center  
    colPropertyType Prop Type   85
    colCity City    
    colState State    
    colCondition Condition    
    colBedrooms Beds Right 65
    colBathrooms Baths Right 65
    colStories Stories Right 75
    colYearBuilt Year Right 70
    colMarketValue Value Right  
    Button Button Close btnClose Anchor: Bottom, Right
  8. Double-click an unoccupied area of the form and implement the Load event as follows:
    using System
    using System.Collections.Generic
    using System.ComponentModel
    using System.Data
    using System.Drawing
    using System.Linq
    using System.Text
    using System.Windows.Forms
    using System.IO
    using System.Runtime.Serialization.Formatters.Binary
    
    namespace AltairRealtors1
    {
        public partial class AltairRealtors : Form
        {
            public AltairRealtors()
            {
                InitializeComponent()
            }
    
            private void AltairRealtors_Load(object sender, EventArgs e)
            {
                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()
                }
            }
        }
    }
  9. Execute the application to create the list
  10. Close the form and return to your programming environment
  11. Return to the form and double-click the Close button
  12. Change the file as follows:
    using System
    using System.Collections.Generic
    using System.ComponentModel
    using System.Data
    using System.Drawing
    using System.Linq
    using System.Text
    using System.Windows.Forms
    using System.IO
    using System.Runtime.Serialization.Formatters.Binary
    
    namespace AltairRealtors1
    {
        public partial class AltairRealtors : Form
        {
            Property() lstProperties
    
            public AltairRealtors()
            {
                InitializeComponent()
            }
    
            private void AltairRealtors_Load(object sender, EventArgs e)
            {
                FileStream stmProperties = null
                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
                        lstProperties = (Property())bfmProperties.Deserialize(stmProperties)
                    }
                    finally
                    {
                        stmProperties.Close()
                    }
                }
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                Close()
            }
        }
    }
 

 

 
 
 
  1. 
    		

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.

If you create an application by selecting the Windows Forms Application option from the New Project dialog box, the studio would add the necessary assemblies to your project and the necessary namespaces to your code file.

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:

Dim SubListName = From ValueHolder In List Select ValueHolder

The Dim 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:

Imports System
Imports System.Linq
Imports System.Drawing
Imports System.Windows.Forms

Module Exercise

    Public Class WinControls
        Inherits Form

        Private lbxNumbers As ListBox

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()
            lbxNumbers = New ListBox
            lbxNumbers.Location = New System.Drawing.Point(12, 12)
            lbxNumbers.Width = 100

            Text = "Numbers"
            MinimizeBox = False
            MaximizeBox = False
            Controls.Add(lbxNumbers)
            Size = New System.Drawing.Size(130, 145)
            StartPosition = FormStartPosition.CenterScreen
        End Sub

        Private Sub ExerciseLoad(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            Dim Numbers() = {12.44, 525.38, 6.28, 2448.32, 632.04}

            Dim Nbrs = From N In Numbers Select N

            For Each Member In Nbrs
                lbxNumbers.Items.Add(Member)
            Next
        End Sub

        Public Shared Function Main() As Integer

            Application.Run(New WinControls())
            Return 0

        End Function

    End Class

End Module

This would produce:

Numbers

To make the code easier to read, you can spread the select statement to various lines. Here is an example:

Dim Nbrs = From N
           In Numbers
           Select N
 
 
   
 

Home Copyright © 2010-2016, FunctionX Next