LINQ Keywords: new |
|
Description |
The new keyword is used to combine a few fields or properties of a class. When using it, after the select keyword, type new followed by curly brackets. In the curly brackets, write the expression. Here is an example:
using System;
using System.Linq;
public class Employee
{
public int EmployeeNumber;
public string FirstName;
public string LastName;
public decimal HourlySalary;
public Employee(int number = 0,
string firstName = "John",
string lastName = "Doe",
decimal salary = 0M)
{
EmployeeNumber = number;
FirstName = firstName;
LastName = lastName;
HourlySalary = salary;
}
}
public class Exercise
{
public static int Main()
{
var employees = new Employee[]
{
new Employee(971974, "Patricia", "Katts", 24.68M),
new Employee(208411, "Raymond", "Kouma", 20.15M),
new Employee(279374, "Hél�ne", "Mukoko", 15.55M),
new Employee(707912, "Bertrand", "Yamaguchi", 24.68M),
new Employee(971394, "Gertrude", "Monay", 20.55M)
};
var fullNames = from empls
in employees
select new
{
FullName = empls.LastName + ", " + empls.FirstName
};
Console.WriteLine("+=====================+");
Console.WriteLine(" Full Names ");
Console.WriteLine("----------------------");
foreach (var staff in fullNames)
Console.WriteLine(" {0}", staff.FullName);
Console.WriteLine("+=====================+");
Console.WriteLine();
return 0;
}
}
This would produce:
+=====================+ Full Names ---------------------- Katts, Patricia Kouma, Raymond Mukoko, Hél�ne Yamaguchi, Bertrand Monay, Gertrude +=====================+ Press any key to continue . . .
Here is another example:
using System;
using System.Linq;
using System.Collections.Generic;
public class Exercise
{
static Employee[] employees;
public static int Main()
{
employees = new Employee[]
{
new Employee(971974, "Patricia", "Katts", 24.68M),
new Employee(208411, "Raymond", "Kouma", 20.15M),
new Employee(279374, "Hél�ne", "Mukoko", 15.55M),
new Employee(707912, "Bertrand", "Yamaguchi", 24.68M),
new Employee(971394, "Gertrude", "Monay", 20.55M)
};
var staffMembers = from empls
in employees
select new
{
empls.EmployeeNumber,
FullName = empls.LastName + ", " + empls.FirstName,
empls.HourlySalary
};
Console.WriteLine("+========+=====================+========+");
Console.WriteLine("| Empl # | Full Name | Salary |");
foreach (var staff in staffMembers)
{
Console.WriteLine("+--------+---------------------+--------+");
Console.WriteLine("| {0,6} | {1,-19} | {2,6} |", staff.EmployeeNumber,
staff.FullName, staff.HourlySalary);
}
Console.WriteLine("+========+=====================+========+");
Console.WriteLine();
return 0;
}
}
public class Employee
{
public int EmployeeNumber;
public string FirstName;
public string LastName;
public decimal HourlySalary;
public Employee(int number = 0,
string firstName = "John",
string lastName = "Doe",
decimal salary = 0M)
{
EmployeeNumber = number;
FirstName = firstName;
LastName = lastName;
HourlySalary = salary;
}
internal string GetFullName()
{
return LastName + ", " + FirstName;
}
}
This would produce:
Using the new keyword, you can create a name for each field and assign it the member of the class that holds the actual fields. Here are examples:
public class Exercise { static Employee[] employees; public static int Main() { employees = new Employee[] { new Employee(971974, "Patricia", "Katts", 24.68M), new Employee(208411, "Raymond", "Kouma", 20.15M), new Employee(279374, "Hél�ne", "Mukoko", 15.55M), new Employee(707912, "Bertrand", "Yamaguchi", 24.68M), new Employee(971394, "Gertrude", "Monay", 20.55M) }; var staffMembers = from empls in employees select new { Number = empls.EmployeeNumber, FName = empls.FirstName, LName = empls.LastName, Wage = empls.HourlySalary }; Console.WriteLine("+========+============+===========+========+"); Console.WriteLine("| Empl # | First Name | Last Name | Salary |"); foreach (var staff in staffMembers) { Console.WriteLine("+--------+------------+-----------+--------+"); Console.WriteLine("| {0,6} | {1,-10} | {2,-9} | {3,6} |", staff.Number, staff.FName, staff.LName, staff.Wage); } Console.WriteLine("+=======+============+===========+=========+"); Console.WriteLine(); return 0; } }
|
||
Previous | Copyright © 2008-2016, FunctionX, Inc. | Next |
|