using System; using System.Linq; public class Exercise { public static int Main() { var names = new string[5]; names[0] = "Patricia Katts"; names[1] = "Raymond Kouma"; names[2] = "Hél�ne Mukoko"; names[3] = "Paul Bertrand Yamaguchi"; names[4] = "Gertrude Monay"; var name = from n in names select n; foreach (var member in name) Console.WriteLine("Member: {0}", member.ToString()); return 0; } } This would produce:
The values used in a LINQ statement can also come from a class. For example, instead of using one of the primitive types to create a list, you can use your own class. Here is an example: public class Employee { public int EmployeeNumber; public string FirstName; public string LastName; public double HourlySalary; } You primarily use the class as you would any other. In your LINQ statement, you can refer to all members of the collection: using System;
using System.Linq;
public class Employee
{
public int EmployeeNumber;
public string FirstName;
public string LastName;
public double HourlySalary;
}
public class Exercise
{
public static int Main()
{
var empls = new Employee[5];
empls[0] = new Employee();
empls[0].EmployeeNumber = 971974;
empls[0].FirstName = "Patricia";
empls[0].LastName = "Katts";
empls[0].HourlySalary = 24.68;
empls[1] = new Employee();
empls[1].EmployeeNumber = 208411;
empls[1].FirstName = "Raymond";
empls[1].LastName = "Kouma";
empls[1].HourlySalary = 20.15;
empls[2] = new Employee();
empls[2].EmployeeNumber = 279374;
empls[2].FirstName = "Hél�ne";
empls[2].LastName = "Mukoko";
empls[2].HourlySalary = 15.55;
empls[3] = new Employee();
empls[3].EmployeeNumber = 707912;
empls[3].FirstName = "Bertrand";
empls[3].LastName = "Yamaguchi";
empls[3].HourlySalary = 24.68;
empls[4] = new Employee();
empls[4].EmployeeNumber = 971394;
empls[4].FirstName = "Gertrude";
empls[4].LastName = "Monay";
empls[4].HourlySalary = 20.55;
var staff = from n in empls select n;
return 0;
}
}
In this case, the value of the select expression represents the whole variable, which are all members of the collection. If you want to get a property (or a member) of the class, apply the period operator to the value of select and access the desired member. Here is an example: using System;
using System.Linq;
public class Employee
{
public int EmployeeNumber;
public string FirstName;
public string LastName;
public double HourlySalary;
}
public class Exercise
{
public static int Main()
{
var empls = new Employee[5];
. . . No Change
var lastNames = from n
in empls
select n.LastName;
foreach (var member in lastNames)
Console.WriteLine("Member: {0}", member.ToString());
return 0;
}
}
This would produce:
This technique allows you to access only one member of the class. As mentioned already, the select statement primarily produces the whole collection of the values of the variable. Since this value represents a collection, you can use it in a list-based such scenario. In this case, to access a member of the class, use a for or foreach loop to get each item of the collection variable and apply the period operator on that value.
To perform a more particular operation on a class, you can create a method in it and then call that method in your LINQ statement. This means that, just as you can access a field or a property of a class, you can access any of its internal or public methods. Here is an example of a method created in a class: using System;
using System.Linq;
public class Employee
{
public int EmployeeNumber;
public string FirstName;
public string LastName;
public double HourlySalary;
public Employee(int number = 0,
string firstName = "John",
string lastName = "Doe",
double salary = 0D)
{
EmployeeNumber = number;
FirstName = firstName;
LastName = lastName;
HourlySalary = salary;
}
internal string GetFullName()
{
return LastName + ", " + FirstName;
}
}
public class Exercise
{
public static int Main()
{
var empls = new Employee[5];
empls[0] = new Employee();
empls[0].EmployeeNumber = 971974;
empls[0].FirstName = "Patricia";
empls[0].LastName = "Katts";
empls[0].HourlySalary = 24.68;
empls[1] = new Employee();
empls[1].EmployeeNumber = 208411;
empls[1].FirstName = "Raymond";
empls[1].LastName = "Kouma";
empls[1].HourlySalary = 20.15;
empls[2] = new Employee();
empls[2].EmployeeNumber = 279374;
empls[2].FirstName = "Hél�ne";
empls[2].LastName = "Mukoko";
empls[2].HourlySalary = 15.55;
empls[3] = new Employee();
empls[3].EmployeeNumber = 707912;
empls[3].FirstName = "Bertrand";
empls[3].LastName = "Yamaguchi";
empls[3].HourlySalary = 24.68;
empls[4] = new Employee();
empls[4].EmployeeNumber = 971394;
empls[4].FirstName = "Gertrude";
empls[4].LastName = "Monay";
empls[4].HourlySalary = 20.55;
var fullNames = from names
in empls
select names.GetFullName();
foreach (var member in fullNames)
Console.WriteLine("Member: {0}", member.ToString());
Console.WriteLine();
return 0;
}
}
This would produce:
There are two types of built-in classes you can use in your application when it comes to LINQ. You can use any of the non-generic collection classes to create a list of values. The other category is the generic collection classes. We mentioned that the List factor of our formula could be an array. It can also be a collection-based variable; that is, a variable created from a collection-based class. When creating a LINQ expression, the collection class you use must implement the IEnumerable generic interface. If you want, you can create your own class that implements this interface but the .NET Framework provides a complete set of classes that should suit every need. One of the built-in generic classes of the .NET Framework is called List and you can easily use it to create a list of values. Here is an example: private void Exercise_Load(object sender, EventArgs e) { var numbers = new List<double>(); numbers.Add(12.44); numbers.Add(525.38); numbers.Add(6.28); numbers.Add(2448.32); numbers.Add(632.04); } After creating the collection, you can use the same LINQ formula we saw for an array. Here is an example: using System; using System.Linq; using System.Collections.Generic; public class Exercise { public static int Main() { var numbers = new List<double>(); numbers.Add(12.44); numbers.Add(525.38); numbers.Add(6.28); numbers.Add(2448.32); numbers.Add(632.04); var number = from n in numbers select n; foreach (var nbr in number) Console.WriteLine("Member: {0}", nbr); Console.WriteLine(); return 0; } } This would produce the same result as seen earlier. Notice that, as always, the var keyword does not itself indicate the type of variable it is dealing with.
|
|||||||||||||||||||||||||||||