Exploring the LINQ |
|
The IEnumerable Generic Interface |
Introduction |
In our introduction to LINQ, we saw how to create a LINQ statement by declaring a variable using the var keyword. Here is an example: using System; using System.Linq; public class Program { static int Main(string[] args) { var People = new string[] { "Paulette Cranston", "Harry Kumar", "Jules Davidson", "Leslie Harrington", "Ernest Colson", "Patricia Katts", "Patrice Abanda", "Frank Thomasson" }; var Persons = from Pers in People select Pers; foreach (var Individual in Persons) Console.WriteLine("Person: {0}", Individual); return 0; } } |
This would produce: Person: Paulette Cranston Person: Harry Kumar Person: Jules Davidson Person: Leslie Harrington Person: Ernest Colson Person: Patricia Katts Person: Patrice Abanda Person: Frank Thomasson Press any key to continue . . . From what we have learned so far, the only thing we know from this code is that it gives us a list and we can display the values of this list to the console. At times, we may want to get more information about some characteristics of the result, not simply its values. To assist you with exploring the characteristics of a LINQ statement, the .NET Framework provides the IEnumerable interface.
In all of the statements we have created/written so far, we used the var keyword, which allowed us to let the compiler figure out what type of value the statement would produce. In reality, a LINQ statement is an object of type IEnumerable. The IEnumerable object used in LINQ is an interface defined in the System.Collections.Generic namespace defined in the mscorlib.dll assembly. This also means that you want to use it, you should include the System.Collections.Generic namespace in your code file. Since a LINQ statement produces a list of type IEnumerable, instead of the var keyword, you can declare the statement using that. Inside the <> operator, enter the type of list that the collection variable contains. Here is an example: using System; using System.Linq; using System.Collections.Generic; public class Program { static int Main(string[] args) { var People = new string[] { "Paulette Cranston", "Harry Kumar", "Jules Davidson", "Leslie Harrington", "Ernest Colson", "Patricia Katts", "Patrice Abanda", "Frank Thomasson" }; IEnumerable<string> Persons = from Pers in People select Pers; foreach (var Individual in Persons) Console.WriteLine("Person: {0}", Individual); return 0; } } This statement is the same as the previous except that the var keyword has been replaced with IEnumerable<string> and it will produce the same result we had earlier.
As mentioned already, when you create a LINQ statement, it produces a list. Although the list is of type IEnumerable, since this is only an interface, the result relies on an actual class to provide its characteristics. The class that gives you information about a result is called Enumerable. The Enumerable class in defined in the System.Linq namespace that is part of the System.Core.dll assembly. It is actually the Enumerable class that implements the methods declared in the IEnumerable interface. Because the Enumerable class is extremely big, we cannot review all of its methods. We will use them as we move on and when a particular method becomes necessary. As mentioned already, a LINQ statement produces an Enumerable list. You can then use that result to access a method of the class. For example, the IEnumerable.Count() method is used to know the number of items in the resulting list. You can access it from the resulting list. Here is an example: using System; using System.Linq; using System.Collections.Generic; public class Program { static int Main(string[] args) { var People = new string[] { "Paulette Cranston", "Harry Kumar", "Jules Davidson", "Leslie Harrington", "Ernest Colson", "Patricia Katts", "Patrice Abanda", "Frank Thomasson" }; IEnumerable<string> Persons = from Pers in People select Pers; int Total = Persons.Count(); Console.Write("There are {0} people in the list and they are: ", Total); foreach (var Individual in Persons) Console.Write("{0}, ", Individual); Console.WriteLine(); return 0; } } This would produce: There are 8 people in the list and they are: Paulette Cranston, Harry Kumar, Jul es Davidson, Leslie Harrington, Ernest Colson, Patricia Katts, Patrice Abanda, F rank Thomasson, Press any key to continue . . . Remember that you can still use the var keyword to declare the variable that would hold the resulting list. Since we have determined that the LINQ statement produces an Enumerable object, if you don't need the list itself, you can declare a variable that is the type returned by a method, put the statement in parentheses, and then access the method outside the closing parenthesis using the period operator. Here is an example: using System; using System.Linq; using System.Collections.Generic; public class Program { static int Main(string[] args) { var People = new string[] { "Paulette Cranston", "Harry Kumar", "Jules Davidson", "Leslie Harrington", "Ernest Colson", "Patricia Katts", "Patrice Abanda", "Frank Thomasson" }; int Total = (from Pers in People select Pers).Count(); Console.Write("There are {0} people in the list.", Total); Console.WriteLine(); return 0; } } There are 8 people in the list. Press any key to continue . . . Remember that the IEnumerable.Count() method returns the number of items in the result of the LINQ statement, not the number of items in the original list. The following examples illustrate it: using System; using System.Linq; public class Person { public string FirstName; public string LastName; public char Gender; public Person(string First, string Last, char Sex) { FirstName = First; LastName = Last; Gender = Sex; } } public class Program { static int Main(string[] args) { var People = new Person[] { new Person("Paulette", "Cranston", 'F'), new Person("Harry", "Kumar", 'M'), new Person("Jules", "Davidson", 'M'), new Person("Leslie", "Harrington", 'U'), new Person("Ernest", "Colson", 'M'), new Person("Patricia", "Katts", 'F'), new Person("Patrice", "Abanda", 'U'), new Person("Frank", "Thomasson", 'M') }; var AllPeople = from Pers in People select Pers; int Men = (from Males in AllPeople where Males.Gender == 'M' select Males).Count(); int Women = (from Females in AllPeople where ((Females.Gender != 'M') && (Females.Gender != 'U')) select Females).Count(); Console.Write("The original list contains {0} people and they are: ", AllPeople.Count()); foreach (var Individual in AllPeople) Console.Write("{0}, ", Individual); Console.WriteLine(); Console.WriteLine("This list contains {0} men and {1} women.", Men, Women); return 0; } } This would produce: The original list contains 8 people and they are: Person, Person, Person, Person , Person, Person, Person, Person, This list contains 4 men and 2 women. Press any key to continue . . .
|
|
||
Previous | Copyright © 2008-2016, FunctionX, Inc. | Next |
|