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

Start Microsoft Visual Studio or Microsoft Visual Basic 2010
Express

Create a Console Application named AltairRealtors1

To create a new class, in the Class View, right-click
AltairRealtors1 -> Add -> Class...

Change the name to House and press Enter

Complete the class as follows:

Public Enum PropertyType
Condominium
Townhouse
SingleFamily
Unknown
End Enum
Public Enum PropertyCondition
Unknown
Excellent
Good
NeedsRepair
BadShape
End Enum
<Serializable()>
Public Class House
Private nbr As Integer
Private tp As PropertyType
Private ct As String
Private stt As String
Private cond As PropertyCondition
Private beds As Short
Private baths As Single
Private levels As Integer
Private yr As Integer
Private val As Decimal
Public Sub New()
nbr = 0
tp = PropertyType.Unknown
ct = "Unknown"
stt = "AA"
cond = PropertyCondition.Unknown
beds = 0
baths = 0.0
levels = 0
yr = 1900
val = 0
End Sub
Public Sub New(ByVal propNbr As Integer, ByVal type As PropertyType,
ByVal city As String, ByVal state As String,
ByVal condition As PropertyCondition, ByVal bedrooms As Short,
ByVal bathrooms As Single, ByVal stories As Integer,
ByVal year As Integer, ByVal value As Decimal)
nbr = propNbr
tp = type
ct = city
stt = state
cond = condition
beds = bedrooms
baths = bathrooms
levels = stories
yr = year
val = value
End Sub
Public Property PropertyNumber As Integer
Get
Return nbr
End Get
Set(ByVal value As Integer)
nbr = value
End Set
End Property
Public Property Type As PropertyType
Get
Return tp
End Get
Set(ByVal value As PropertyType)
tp = value
End Set
End Property
Public Property City As String
Get
Return ct
End Get
Set(ByVal value As String)
ct = value
End Set
End Property
Public Property State As String
Get
Return stt
End Get
Set(ByVal value As String)
stt = value
End Set
End Property
Public Property Condition As PropertyCondition
Get
Return cond
End Get
Set(ByVal value As PropertyCondition)
cond = value
End Set
End Property
Public Property Bedrooms As Short
Get
If beds <= 1 Then
Return 1
Else
Return beds
End If
End Get
set (value as short)
beds = value
End Set
End Property
public property Bathrooms as single
Get
If baths <= 0 Then
Return 0
Else
Return baths
End If
End Get
Set(ByVal value As Single)
baths = value
End Set
End Property
Public Property Stories As Integer
Get
Return levels
End Get
Set(ByVal value As Integer)
levels = value
End Set
End Property
Public Property YearBuilt As Integer
Get
Return yr
End Get
Set(ByVal value As Integer)
yr = value
End Set
End Property
Public Property MarketValue As Decimal
Get
If val <= 0 Then
Return 0
Else
Return val
End If
End Get
Set(ByVal value As Decimal)
val = value
End Set
End Property
End Class

Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Module AltairRealtors
Private lstProperties() As House
Function Main() As Integer
Dim stmProperties As FileStream = Nothing
Dim bfmProperties As BinaryFormatter = New BinaryFormatter
' This is the file that holds the list of properties
Dim Filename As String = "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) Then
stmProperties = New FileStream(Filename,
FileMode.Open,
FileAccess.Read,
FileShare.Read)
Try
' Retrieve the list of items from file
lstProperties = CType(bfmProperties.Deserialize(stmProperties), House())
Finally
stmProperties.Close()
End Try
End If
Return 0
End Function
End Module

Querying a List

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

Click OK. You must then use the System.Linq
namespace in your code or you can import it.

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:

DimSubListName= FromValueHolderInListSelectValueHolder

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: