In the LINQ, the let operator is used to declare a local variable in a query statement. To specify the value of the variable, you can create an expression that combines the members of the class and assign that expression to the let variable. To end your query, select that variable. Outside the query, access that variable from the var variable of the foreach loop. Here is an example: using System;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
public class Video
{
public int ShelfNumber { get; set; }
public string Title { get; set; }
public string Director { get; set; }
public string Rating { get; set; }
public int YearReleased { get; set; }
public bool WideScreen { get; set; }
public Video(int id = 0,
string title = "",
string dir = "",
string rating = "",
int year = 0,
bool hd = false)
{
ShelfNumber = id;
Title = title;
Director = dir;
Rating = rating;
YearReleased = year;
WideScreen = hd;
}
}
public class VideoCollection : Form
{
private ListBox cbxVideos;
public VideoCollection()
{
InitializeComponent();
}
void InitializeComponent()
{
cbxVideos = new ListBox();
cbxVideos.Location = new System.Drawing.Point(12, 12);
cbxVideos.Size = new Size(380, 120);
Controls.Add(cbxVideos);
Text = "Video Collection";
Size = new System.Drawing.Size(412, 158);
Controls.Add(cbxVideos);
Load += new EventHandler(VideoCollectionLoad);
}
private void VideoCollectionLoad(object sender, EventArgs e)
{
Video[] lstVideos = new Video[]
{
new Video(274880, "Platoon", "Oliver Stone", "R", 1986, false),
new Video(792075, "Two for the Money", "D.J. Caruso", "R", 2008, true),
new Video(283748, "Cousin Vinny (My)", "Jonathan Lynn", "R", 1992, false),
new Video(593940, "Natural Born Killers", "Oliver Stone", "R", 1994, true),
new Video(900245, "Her Alibi", "Bruce Beresford", "PG-13", 1998, false),
new Video(773022, "Distinguished Gentleman (The)", "Jonathan Lynn", "R", 1992, false),
new Video(961973, "Wall Street", "Oliver Stone", "R", 2000, false),
new Video(180358, "Memoirs of a Geisha", "Rob Marshall", "PG-13", 2006, true)
};
var vdos = from videos
in lstVideos
let video = "\"" + videos.Title + "\" directed by " + videos.Director + ", released in " + videos.YearReleased.ToString()
select video;
foreach (var vdo in vdos)
cbxVideos.Items.Add(vdo);
}
[STAThread]
public static int Main()
{
System.Windows.Forms.Application.Run(new VideoCollection());
return 0;
}
}
This would produce:
If you need a condition, add a where statement before or after the let expressions. Here is an example: var vdos = from videos
in lstVideos
where videos.Director == "Oliver Stone"
let video = "\"" + videos.Title + "\" directed by " + videos.Director + ", released in " + videos.YearReleased.ToString()
select video;
Of course, you can order the list and/or use a conjunction or disjunction.
As we have seen above, if you query a list whose records are based on a class, you can access the members (properties and methods) of the class both in the query and in the foreach loop. As an alternative, the LINQ allows you to declare various variables in the query and access those variables in the foreach loop. To declared such variables, start with select new followed by "{" and "}": var SubListName = from ValueHolder in List select new { ValueHolder.ClassMember1 = Value1 ValueHolder.ClassMember1 = Value2 ValueHolder.ClassMember_n = Value_n }; Inside the curly brackets, declare each variable and assign it the desired value. The value can be a member of the class. Here is an example: var vdos = from videos
in lstVideos
select new
{
ReferenceCode = videos.ShelfNumber
};
Actually, the variable can hold the same name as a member of the class. In the same way, you can declare as many variables as you want and assign the desired class members to them. Separate the declarations with commas. Outside the query, to access a new variable, qualify it with a period operator applied to the foreach variable. Here are examples: var vdos = from videos
in lstVideos
select new
{
ReferenceCode = videos.ShelfNumber,
Name = videos.Title,
Director = videos.Director,
Viewership = videos.Rating,
CopyrightYear = videos.YearReleased,
WideScreen = videos.WideScreen,
};
foreach (var vdo in vdos)
{
ListViewItem lviCollection = new ListViewItem(vdo.ReferenceCode.ToString());
lviCollection.SubItems.Add(vdo.Name);
lviCollection.SubItems.Add(vdo.Director);
lviCollection.SubItems.Add(vdo.Viewership);
lviCollection.SubItems.Add(vdo.CopyrightYear.ToString());
lviCollection.SubItems.Add(vdo.WideScreen.ToString());
lvwCollection.Items.Add(lviCollection);
}
This would produce:
Instead of one value, the value of a new variable can be an expression that is a combination of values (constants and/or class members). Here are examples: using System;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
public class Video
{
public int ShelfNumber { get; set; }
public string Title { get; set; }
public string Director { get; set; }
public string Rating { get; set; }
public int YearReleased { get; set; }
public bool WideScreen { get; set; }
public Video(int id = 0,
string title = "",
string dir = "",
string rating = "",
int year = 0,
bool hd = false)
{
ShelfNumber = id;
Title = title;
Director = dir;
Rating = rating;
YearReleased = year;
WideScreen = hd;
}
}
public class VideoCollection : Form
{
private ColumnHeader colShelfNumber;
private ColumnHeader colDescription;
private ColumnHeader colRating;
private ColumnHeader colWideScreen;
ListView lvwCollection;
public VideoCollection()
{
InitializeComponent();
}
void InitializeComponent()
{
lvwCollection = new ListView();
lvwCollection.Anchor = AnchorStyles.Left | AnchorStyles.Top |
AnchorStyles.Right | AnchorStyles.Bottom;
lvwCollection.FullRowSelect = true;
lvwCollection.GridLines = true;
lvwCollection.Location = new Point(12, 12);
lvwCollection.Size = new System.Drawing.Size(505, 137);
lvwCollection.View = View.Details;
colShelfNumber = new ColumnHeader();
colShelfNumber.Text = "Shelf #";
colShelfNumber.Width = 50;
lvwCollection.Columns.Add(colShelfNumber);
colDescription = new ColumnHeader();
colDescription.Text = "Video Description";
colDescription.Width = 320;
lvwCollection.Columns.Add(colDescription);
colRating = new ColumnHeader();
colRating.Text = "Rating";
colRating.Width = 50;
colRating.TextAlign = HorizontalAlignment.Center;
lvwCollection.Columns.Add(colRating);
colWideScreen = new ColumnHeader();
colWideScreen.Text = "Wide Screen?";
colWideScreen.Width = 80;
colWideScreen.TextAlign = HorizontalAlignment.Center;
lvwCollection.Columns.Add(colWideScreen);
Text = "Video Collection";
MaximizeBox = false;
Size = new System.Drawing.Size(545, 188);
Controls.Add(lvwCollection);
Load += new EventHandler(VideoCollectionLoad);
}
private void VideoCollectionLoad(object sender, EventArgs e)
{
Video[] lstVideos = new Video[]
{
new Video(274880, "Platoon", "Oliver Stone", "R", 1986, false),
new Video(792075, "Two for the Money", "D.J. Caruso", "R", 2008, true),
new Video(283748, "Cousin Vinny (My)", "Jonathan Lynn", "R", 1992, false),
new Video(593940, "Natural Born Killers", "Oliver Stone", "R", 1994, true),
new Video(900245, "Her Alibi", "Bruce Beresford", "PG-13", 1998, false),
new Video(773022, "Distinguished Gentleman (The)", "Jonathan Lynn", "R", 1992, false),
new Video(961973, "Wall Street", "Oliver Stone", "R", 2000, false),
new Video(180358, "Memoirs of a Geisha", "Rob Marshall", "PG-13", 2006, true)
};
var vdos = from videos
in lstVideos
select new
{
ReferenceCode = videos.ShelfNumber,
Description = videos.Title + " (" + videos.YearReleased + ") directed by " + videos.Director,
Rating = videos.Rating,
WideScreen = videos.WideScreen,
};
foreach (var vdo in vdos)
{
ListViewItem lviCollection = new ListViewItem(vdo.ReferenceCode.ToString());
lviCollection.SubItems.Add(vdo.Description);
lviCollection.SubItems.Add(vdo.Rating.ToString());
lviCollection.SubItems.Add(vdo.WideScreen.ToString());
lvwCollection.Items.Add(lviCollection);
}
}
[STAThread]
public static int Main()
{
System.Windows.Forms.Application.Run(new VideoCollection());
return 0;
}
}
This would produce:
|
|
|||||||
|
|