Showing posts with label sample. Show all posts
Showing posts with label sample. Show all posts

Sunday, May 25, 2008

Visual Studio 2008 - LINQ Ordering Operators Sample

OrderBy - Simple
This sample prints an alphabetically sorted version of an input string array. The sample uses orderby to perform the sort.
publicvoid Linqsample()
{
string[] colors = { "Blue", "red", "green" };
var sortedcolorss = from c in colors
orderby c
select c;
Console.WriteLine("The sorted list of colors:");
foreach (var c in sortedcolors)
{
Console.WriteLine(c);
}
}
Result
The sorted list of words:
Blue
green
red

Saturday, May 24, 2008

Visual Studio 2008 - LINQ Projection Operators Sample

Select - Simple
This sample prints a integers Hundred greater than those in an input array. The sample uses the expression in the select clause to add hundred to each element in the new sequence.

public void Linqsample()

{

int[] numbers = { 500, 400, 100, 300, 900, 800, 600, 700, 200, 000 };

var numsPlusOne = from n in numbers

select n + 100;

Console.WriteLine("Numbers + 100:");

foreach (var i in numsPlusHundred)

{ Console.WriteLine(i); }

}
Result
Numbers + 1:

600

500

200

400

1000

900

700

800

300

100

Visual Studio 2008 - LINQ Restriction Operators Sample

Where - Simple 1
This sample prints each element of an input integer array whose value is less than 500. The sample uses a query expression to create a new sequence of integers and then iterates over each element in the sequence, printing its value.


public void LinqSample()
{
int[] arr= { 500, 400, 100, 300, 900, 800, 600, 700, 200, 000 };
var lowNums = from result in arr
where result < 500
select result ;
Console.WriteLine("arr < 500:");
foreach (var n in lowNums)
{
Console.WriteLine(n);
}
}
Result:
arr < 500:
400
100
300
200
000