PART I
C h a p t e r 1 9 :
L I N Q
547
PART IPART I
// Create a query that obtains only positive numbers.
var posNums = from n in nums
where n > 0
select n;
Console.Write("The positive values in nums: ");
// Execute the query and display the results.
foreach(int i in posNums) Console.Write(i + " ");
Console.WriteLine();
}
}
This program produces the following output:
The positive values in nums: 1 3 5
As you can see, only the positive values in the
nums
array are displayed. Although quite
simple, this program demonstrates the key features of LINQ. Let’s examine it closely.
The first thing to notice in the program is the
using
directive:
using System.Linq;
To use the LINQ features, you must include the
System.Linq
namespace.
Next, an array of
Do'stlaringiz bilan baham: |