LINQ C# Book

Input: IEnumerable
Lambda expression: TSource => TKey
The following query emits a sequence of names in alphabetical order:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
string[] names = { "Java", "C#", "Javascript", "SQL", "Oracle", "Python", "C++", "C", "HTML", "CSS" };
IEnumerable query = names.OrderBy(s => s);
foreach(String s in query){
Console.WriteLine(s);
}
}
}
The output:
C
C#
C++
CSS
HTML
Java
Javascript
Oracle
Python
SQL
The following sorts names by length:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
string[] names = { "Java", "C#", "Javascript", "SQL", "Oracle", "Python", "C++", "C", "HTML", "CSS" };
IEnumerable query = names.OrderBy(s => s.Length);
foreach(String s in query){
Console.WriteLine(s);
}
}
}
The output:
C
C#
SQL
C++
CSS
Java
HTML
Oracle
Python
Javascript
The following performs a case-insensitive sort:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
string[] names = { "Java", "C#", "Javascript", "SQL", "Oracle", "Python", "C++", "C", "HTML", "CSS" };
names.OrderBy(n => n, StringComparer.CurrentCultureIgnoreCase);
foreach (String s in names)
{
Console.WriteLine(s);
}
}
}
The output:
Java
C#
Javascript
SQL
Oracle
Python
C++
C
HTML
CSS