public static IEnumerable The variable that calls this method and the argument must be of the same type. Here is an example: private void btnUnionClick(object sender, EventArgs e)
{
var names = new string[]
{
"Jerrie Sachs",
"Stevens Souza",
"Marianne Swanson",
"Alain Gudmundson",
"Jeannette Perkins",
"Pierrette Perkins"
};
var values = new string[]
{
"Gertrude Monay",
"Raymond Kouma"
};
IEnumerable<string> result = numbers.Union<string>(values);
foreach (var member in result)
lbxNames.Items.Add(member.ToString());
}
In the same way, you can unite records from lists of classes. The most important rule to observe is that both lists must have values that are of the same type.
In all statements we have created so far, we were storing the results in a select variable, and the select variable was able to provide its list when necessary. We also learned how to sort the values in select list. As an alternative, you can ask the compiler to produce a list where the values are grouped by categories. For example, if you have a list of students, you may want the list to be organized by gender, or by another category. To support grouping, the LINQ provides the group...by operation. The primary formula to use it is: var SubListName = from ValueHolder in List group ValueHolder by Category; The new keywords are group and by. They are required. The ValueHolder factor is the same variable created from the from operator. The Category is the new factor of our formula. It must be a value that can be gotten from the ValueHolder list. Here is an example: var videos = from vdos
in lstVideos
group vdos by vdos.Rating;
The group...by expression creates a list of categories, not a list of values, like the select statement would do. This means that each item of the list produced by the group...by clause is a category. Therefore, to access a category, use a foreach loop that applies to each item of the list. Here is an example: var videos = from vdos in lstVideos group vdos by vdos.Rating; foreach (var toView in videos) { } Each category of the group...by list is its own list of items. Therefore, after accessing a category, inside of it, you can access an item. To do this, use a nested foreach loop. Here is an example: using System; using System.Linq; using System.Drawing; using System.Windows.Forms; using System.Collections.Generic; public class Exercise : Form { private Button btnShow; private ColumnHeader colShelfNumber; private ColumnHeader colTitle; private ColumnHeader colDirector; private ColumnHeader colLength; private ColumnHeader colRating; private ColumnHeader colYearReleased; ListView lvwVideos; public Exercise() { InitializeComponent(); } void InitializeComponent() { btnShow = new Button(); btnShow.Location = new System.Drawing.Point(12, 12); btnShow.Width = 75; btnShow.Text = "Show"; btnShow.Click += new System.EventHandler(this.btnShow_Click); lvwVideos = new ListView(); lvwVideos.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom; lvwVideos.FullRowSelect = true; lvwVideos.GridLines = true; lvwVideos.Location = new Point(12, 40); lvwVideos.Size = new System.Drawing.Size(490, 192); lvwVideos.View = View.Details; colShelfNumber = new ColumnHeader(); colShelfNumber.Text = "Shelf #"; colShelfNumber.Width = 50; lvwVideos.Columns.Add(colShelfNumber); colTitle = new ColumnHeader(); colTitle.Text = "Title"; colTitle.Width = 160; lvwVideos.Columns.Add(colTitle); colDirector = new ColumnHeader(); colDirector.Text = "Director"; colDirector.Width = 130; lvwVideos.Columns.Add(colDirector); colLength = new ColumnHeader(); colLength.Text = "Length"; colLength.Width = 50; colLength.TextAlign = HorizontalAlignment.Right; lvwVideos.Columns.Add(colLength); colRating = new ColumnHeader(); colRating.Text = "Rating"; colRating.Width = 50; colRating.TextAlign = HorizontalAlignment.Center; lvwVideos.Columns.Add(colRating); colYearReleased = new ColumnHeader(); colYearReleased.Text = "Year"; colYearReleased.Width = 40; colYearReleased.TextAlign = HorizontalAlignment.Right; lvwVideos.Columns.Add(colYearReleased); Size = new System.Drawing.Size(520, 270); Controls.Add(this.btnShow); Controls.Add(this.lvwVideos); Text = "Students Records"; } private void btnShow_Click(object sender, EventArgs e) { Video vdo = null; List<Video> lstVideos = new List<Video>(); lstVideos.Add(new Video("730294", "Two for the Money", "D.J. Caruso", "123", "R", "2008")); lstVideos.Add(new Video("283759", "Her Alibi", "Bruce Beresford", "94", "PG-13", "1998")); vdo = new Video(); vdo.Title = "Distinguished Gentleman (The)"; vdo.Director = "Jonathan Lynn"; vdo.Length = "112"; vdo.Rating = "R"; lstVideos.Add(vdo); lstVideos.Add(new Video("905840", "Memoirs of a Geisha", "Rob Marshall", "145", "PG-13", "2006")); vdo = new Video(); vdo.Title = "Silence of the Lambs (The)"; vdo.Director = "Jonathan Demme"; vdo.YearReleased = "1991"; vdo.Length = "118"; lstVideos.Add(vdo); lstVideos.Add(new Video("849613", "Wall Street", "Oliver Stone", "126", "R", "2000")); vdo = new Video(); vdo.Title = "A Few Good Men"; vdo.Director = "Rob Reiner"; vdo.YearReleased = "1992"; vdo.Length = "138"; lstVideos.Add(vdo); vdo = new Video(); vdo.ShelfNumber = "707461"; vdo.Title = "Michael Jackson Live in Bucharest"; vdo.Director = "Andy Morahan"; vdo.Length = "122"; vdo.Rating = "N/R"; vdo.YearReleased = "1992"; lstVideos.Add(vdo); vdo = new Video(); vdo.ShelfNumber = "479703"; vdo.Title = "Lady Killers (The)"; vdo.Director = "Joel Coen & Ethan Coen"; vdo.Rating = "R"; vdo.Length = "104"; lstVideos.Add(vdo); vdo = new Video(); vdo.Title = "Outfoxed"; vdo.Director = "Robert Greenwald"; lstVideos.Add(new Video("900741", "Sneakers", "Phil Alden Robinson", "126", "PG-13", "1992")); lstVideos.Add(new Video("119411", "Soldier", "Paul Anderson", "99", "R", "1998")); lstVideos.Add(new Video("279357", "War of the Roses (The)", "Dany de Vito", "116", "R", "2001")); lstVideos.Add(new Video("927947", "Last Castle (The)", "Rod Lurie", "133", "R", "2001")); lstVideos.Add(new Video("", "Ghosts of Mississippi", "Rob Reiner", "130", "", "")); var videos = from vdos in lstVideos group vdos by vdos.Rating; foreach (var toView in videos) { foreach (Video item in toView) { ListViewItem lviStudent = new ListViewItem(item.ShelfNumber); lviStudent.SubItems.Add(item.Title); lviStudent.SubItems.Add(item.Director); lviStudent.SubItems.Add(item.Length); lviStudent.SubItems.Add(item.Rating); lviStudent.SubItems.Add(item.YearReleased); lvwVideos.Items.Add(lviStudent); } } } [STAThread] public static int Main() { System.Windows.Forms.Application.Run(new Exercise()); return 0; } } public class Video { public string ShelfNumber { get; set; } public string Title { get; set; } public string Director { get; set; } public string Length { get; set; } public string Rating { get; set; } public string YearReleased { get; set; } public Video(string number = "", string ttl = "", string dir = "", string len = "", string ratings = "", string year = "") { ShelfNumber = number; Title = ttl; Director = dir; Length = len; Rating = ratings; YearReleased = year; } } This would produce:
Notice that you do not create a select statement after the group...by clause. To restrict the list of records in the result, you can add a where condition. Here is an example: var videos = from vdos
in lstVideos
where vdos.Title.StartsWith("S")
group vdos by vdos.Rating;
This would produce:
When you create grouping of values, the resulting list is stored in a variable of type IGrouping. The IGrouping interface is defined in the System.Linq namespace of the System.Core.dll assembly. The IGrouping interface is derived from the IEnumerable interface. This means that it gets most of its behaviors from that interface. This also means that using the IGrouping interface gives you access to the members of the Enumerable class. The IGrouping interface is a generic class declared as follows: public interface IGrouping<TKey, TElement> : IEnumerable<TElement>, IEnumerable In our introduction to grouping, we saw that its operation identifies the categories of items from the from variable. Each category is referred to as a key and each category can be recognized as a TKey object of the IGrouping list. This allows you to access each category. In fact, you can access a category and perform an operation on it. Although the IGrouping interface inherits most of its functionality from the IEnumerable interface and implemented through the Enumerable class, it is equipped with only one property, named Key. To get the value of an IGrouping category, you can retrieve it from the Key property.
When you create a grouping, you get a list of categories of values and that list becomes ready to be used. In some cases, before exploring the list, you may want to perform an operation on it. One way you can do this, you can store that list in a (local) variable and use that variable as if it were a from variable. To declare a variable to store the grouping values, you use the into contextual keyword through the following formula: var SubListName = from ValueHolder in List group ValueHolder by Category into GroupVariable ...; The GroupVariable is the new factor in our formula. You specify it as a regular name of a variable. Here is an example: var empls = from staffMembers in employees group staffMembers by staffMembers.Gender into Categories After creating the name, you can perform any operation on it inside the LINQ statement. The variable is of type IGrouping. This means that you can access its Key property or you can access one of the methods that the interface gets from IEnumerable, and then use it as you see fit. Here is an example: var empls = from staffMembers in employees group staffMembers by staffMembers.Gender into Categories where Categories.Contains(students[0]) Before ending the LINQ statement, you must create either a group...by expression or a select statement that uses the into variable. Here is an example: var videos = from vdos in lstVideos group vdos by vdos.Rating into categories where categories.Contains(lstVideos[0]) select categories; This statement, particularly the Enumerable.Contains(lstVideos[0]) produces only the category (group) identified as the first index (0) of the values in the main list (students):
Notice that all records in the final result have a common category, which in this case is the R rating of each video. For this reason, you can omit that column when presenting the values to the user. Here is an example (the column for the rating was removed from the list view): var videos = from vdos in lstVideos group vdos by vdos.Rating into categories where categories.Contains(lstVideos[0]) select categories; foreach (var toView in videos) { foreach (Video item in toView) { ListViewItem lviStudent = new ListViewItem(item.ShelfNumber); lviStudent.SubItems.Add(item.Title); lviStudent.SubItems.Add(item.Director); lviStudent.SubItems.Add(item.Length); // lviStudent.SubItems.Add(item.Rating); lviStudent.SubItems.Add(item.YearReleased); lvwVideos.Items.Add(lviStudent); } } This would produce:
In the same, to get the category stored in the second index of the grouping, you would use Enumerable.Contains(lstVideos[1]). Of course this means that you can use grouping and the into operator to get a list of items of only one particular category. Although the GroupVariable can be selected or grouped...by, it cannot be used outside the LINQ statement. It is only available in the local LINQ expression.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||