|
Introduction to the LINQ |
|
The Language Integrated Query |
|
|
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 and create a Windows Forms
Application named AltairRealtors1
- To create a new class, in the Class View, right-click
AltairRealtors1 -> Add -> Class...
- Change the name to Property and press Enter
- 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 }
}
}
}
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type AltairRealtors.cs and press Enter twice to display the
form
- Design the form as follows:
|
Control |
Text |
Name |
Other Properties |
ListView |
|
|
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 |
|
Close |
btnClose |
Anchor: Bottom, Right |
|
- 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()
}
}
}
}
- Execute the application to create the list
- Close the form and return to your programming environment
- Return to the form and double-click the Close button
- 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()
}
}
}
|
|