Using the LINQ sequence operators,
you can transliterate the description
from the previous paragraph directly
into code:
end that describes what the slice repre-
sents. This means that by defining the
charting API to be LINQ friendly, you
can create charts by writing a query over
the Google image charts API:
var file = System.IO.File.
ReadAllText(“words.txt”);
var words = file.Split(delimiters)
.Where(w⇒
!w.IsNullOrWhiteSpace())
.Select(w⇒
w.ToLower());
Instead of using the sequence operators directly, LINQ also provides a more
“declarative” query comprehension
syntax. Using comprehensions, you can
rewrite the code as follows:
var words =
from w in file.Split(delimiters)
where !w.IsNullOrWhiteSpace()
select w.ToLower();
to truly understand
the power of LinQ,
let’s take a step
back and investigate
its origins and
mathematical
foundations. Don’t
worry, you need
knowledge of only
high school-level
mathematics.
var chart = new Pie(from w in top5
select new Slice( w.Count){ Legend = r.Word })
{Title = “Top 5 words” };
var image = await chart;
Once you have converted the file into
a sequence of individual words, you can
find the number of occurrences of each
word by first grouping the collection by
each word and then counting the num-
ber of elements in each group (which
contains all occurrences of that word):
The await keyword is used in an
unorthodox way to make the expensive
coercion from
Google.Linq.Charts.
Pie into an image that requires a net-
work round-trip explicit.
var wordcount =
from w in words
group by w into group
select new{ Word = group.Key,
Count = group.Count() };
Without using query comprehension
syntax, the code would look like this:
var wordcount = words.GroupBy(w⇒w).
Select(group⇒
new{ Word =
group.Key, Count =
group.Count() };
To find the top five most frequent
words, you can order each record by
Count and take the first five elements:
var top5 = wc.OrderByDescending(p⇒
p.Count).Take( 5);
Now that you have a collection of the
top five words in the file, you can visualize them in a pie chart, as in Figure 1.
A pie chart is really nothing more than
a collection of slices, where each slice
consists of a number that represents
the proportion of the total pie and a leg-
Datacentric interpretation
Relational algebra, which forms the
formal basis for SQL, defines a number of constants and constructors for
sets of values {Σ}, such as the empty set
∅∈{Σ}; injection of a value into a singleton collection {_}∈Σ→{Σ}; and the
union of two sets into a new combined
set ∪∈{Σ}x{Σ}→{Σ}. There are also a
number of relational operators such as
projection, which applies a transformation to each element in a set π∈(Σ→Λ)
x{Σ}→{Λ}; selection, which selects
only those elements in a set that satisfy
a given property σ∈(Σ→)x{Σ}→{Σ};
Cartesian product, which pairs up all
the elements of a pair of sets X∈{Σ}
x{Λ}→{ΣxΛ}; and cross-apply, which
generates a secondary set of values for
each element in a first set ∈(Σ→{Λ})
x{Σ}→{Λ}.
Figure 2 depicts the relational algebra operators using clouds to denote
sets of values. An SQL compiler translates queries expressed in the familiar
SELECT-FROM-WHERE syntax into
relational algebra expressions; to optimize the query it applies algebraic