PART I
C h a p t e r 1 9 :
L I N Q
563
PART IPART I
Because the sequence obtained when
webAddrs
is executed is a list of groups, you will
need to use two
foreach
loops to access the members of each group. The outer loop obtains
each group. The inner loop enumerates the members within the group. The iteration variable
of the outer
foreach
loop must be an
IGrouping
instance compatible with the key and
element type. In the example both the keys and elements are
string
. Therefore, the type of
the
sites
iteration variable of the outer loop is
IGrouping
. The type of the
iteration variable of the inner loop is
string
. For brevity, the example implicitly declares
these variables, but they could have been explicitly declared as shown here:
foreach(IGrouping sites in webAddrs) {
Console.WriteLine("Web sites grouped by " + sites.Key);
foreach(string site in sites)
Console.WriteLine(" " + site);
Console.WriteLine();
}
Do'stlaringiz bilan baham: |