This project is read-only.
groupValBy - Simple 2

Category:
LINQ
Description: This sample uses group by to partition a list of words by their first letter.
Code:
let LINQ24() =
let words = ["blueberry"; "chimpanzee"; "abacus"; "banana"; "apple"; "cheese" ]

query {
for w in words do
groupValBy w (w.[0]) into g
select (g.Key, g.ToArray())
} |> Seq.iter(fun (n,e) -> printfn "Words that start with the letter '%A'" n; (e|>Seq.iter(fun x -> printfn"%s" x)))

//sample output
// Words that start with the letter 'b':
// blueberry
// banana
// Words that start with the letter 'c':
// chimpanzee
// cheese
// Words that start with the letter 'a':
// abacus
// apple



Execution Result:
Words that start with the letter ''b''
blueberry
banana
Words that start with the letter ''c''
chimpanzee
cheese
Words that start with the letter ''a''
abacus
apple

Last edited Sep 14, 2011 at 2:49 AM by ttliu2000, version 1

Comments

No comments yet.