LINQ Keywords: into
The into keyword is combined to a group...by expression to declare a variable to store the grouping values. The formula to follow is:
1 min read76 words2 code samples
Description#
The into keyword is combined to a group...by expression to declare a variable to store the grouping values. The formula to follow is:
C#
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:
C#
using System;
using System.Linq;
using System.Collections.Generic;
public class Exercise
{
public static int Main()
{
var students = new Student[]
{
new Student(82495, "Carlton", "Blanchard", 'M'),
new Student(20857, "Jerrie", "Sachs", 'U'),
new Student(20935, "Charlotte", "O'Keefe", 'F'),
new Student(79274, "Christine", "Burns", 'F'),
new Student(79204, "Bobbie", "Swanson"),
new Student(14815, "Marianne", "Swanson", 'F'),
new Student(24958, "Jeannette", "Perkins", 'F'),
new Student(24759, "Pierrette", "Perkins", 'F'),
new Student(92804, "Charles", "Pressmann", 'M'),
new Student(80074, "Alain", "Goodson", 'M')
};
var classroom = from pupils
in students
group pupils by pupils.Gender into Genders
where Genders.Contains(students[0])
select Genders;
Console.WriteLine("+========+============+===========+=======+");
Console.WriteLine("| Std # | First Name | Last Name | Gender |");
foreach (var stds in classroom)
{
foreach (var pupil in stds)
{
Console.WriteLine("+-------+------------+-----------+--------+");
Console.WriteLine("| {0,5} | {1,-10} | {2,-9} | {3,4} |",
pupil.StudentNumber, pupil.FirstName,
pupil.LastName, pupil.Gender);
}
}
Console.WriteLine("+=======+============+===========+========+");
Console.WriteLine();
return 0;
}
}
public class Student
{
public int StudentNumber;
public string FirstName;
public string LastName;
public char Gender;
public Student(int number = 0,
string firstName = "Leslie",
string lastName = "Doe",
char gdr = 'U')
{
StudentNumber = number;
FirstName = firstName;
LastName = lastName;
Gender = gdr;
}
}This statement, particularly the Enumerable.Contains(students[0]) produces only the category (group) identified as the first index (0) of the values in the main list:
Home Copyright © 2010 FunctionX, Inc.

