Ordering & Grouping
Ordering
Now, we want to order the bird in birds.
from b in birds orderby b.Name select b.Name;
// result: Beo, Kakaktua, Merpati
from b in birds orderby b.Name descending select b.Name;
// result: Merpati, Kakaktua, Beo
from b in birds orderby b.Name, b.Sightings descending select b.Name;
// result: Beo, Kakaktua, Merpati
from b in birds orderby b.Name, b.Sightings descending select new { b.Name, b.Sightings };
// result: {{ Name = Beo, Sightings = 3 },
// { Name = Kakaktua, Sightings = 2 },
// { Name = Merpati, Sightings = 2}}
Grouping
Now, we want to grouping the bird according to a certain parameter.
var birdsByColor = from b in birds group b by b.Color;
foreach (var d in birdsByColor)
Console.WriteLine(d.Key + " " + d.Count());
// result: Red 1 White 1 Black 1
from b in birds group b by b.Color into birdsByColor where birdsByColor.Count = 1
select new { Color = birdsByColor.Key, Count = birdsByColor.Count() };
// result: { { Color = Red, Count = 1 }, { Color = White, Count = 1 }, { Color = Black, Count = 1 } }