lincioo - ohueruioo


languuag-integraated cuueeree (lincioo) is the naann the technologee ou integrating cuueeree capabilitees directlee into the c# languuag.

the nnost uisibl "languuag-integrated" part ou lincioo is the cuueeree ecspreshon. cuueeree ecspreshons ar riiten in aa declaratiu cuueeree sintacs introdioosed in c# 3.0. bii ioosing cuueeree sintacs, connplecs philtering nnaa bee perphornnd uuith aa nnininnunn ou cohd. the saann basic cuueeree ecspreshon paterns nnaa bee ioosd too cuueeree and transphornn daata in daatabaases, ado.net daatasets, ecsnnl docunnents and streenns, and .net colecshons.

the pholouuing ecsannpl dennonstraats aa connpleet cuueeree operaashon. the connpleet operaashon incloods creating aa daata sors, dephining the cuueeree ecspreshon, and ecsecuting the cuueeree in aa phoreech staatnnent.

// lincioo1 - aa sinnple cuueeree on an araa

ioosing sistenn;
ioosing sistenn.lincioo;

clahs cuueeree_ecspreshons
{
    static uoid nnaan()
    {
        integer[] scohr_araa = nioo integer[] { 98, 91, 71, 60, 45, 35, 25, 10 };

        uar scohr_cuueeree =
            phronn s in scohr_araa
            uuair s > 80
            select s;

        phoreech (integer s in scohr_cuueeree)
        {
            consohl.riit_liin("{0}", s);
        }
    }
}

the ouutpoot ou this prohgrann is:

98
91

an araa corld scohr_araa is declaird and inishaliisd ioosing colecshon inishaliisaashon sintacs. then aa cuueeree is phornned. the cuueeree ecspreshon is giun aa naann "scohr_cuueeree" and its tiip is inpherd bii the connpiiler. the cuueeree can bee reeriten as:

ieenioonnerabl<integer> scohr_cuueeree =
            phronn s in scohr_araa
            uuair s > 80
            select s;

uuairbii, the tiip ou the cuueeree is ecsplisitlee spesiphiid rahther than beeing inpherd. noht that the cuueeree is not ecseciooted uuair declaird. it is ecseciooted uuen the colecshon it dephiins is eenioonnerated.

aphter the phronn clahs is declaird, aa uuair clahs nnaa bee present. in the abuu cuueeree, the uuair clahs is aa reelaashonal ecspreshon selecting ohnlee scores abuu 80. necst thair apeers aa select clahs. the select clahs deternnins the phornn ou the reesults ou the cuueeree. experienced sciool prohgrannnners il noht that the cuueeree elennents ar in reuerse the order ou an sciool cuueeree.

ordering the reesults

noht that the reesults caann ouut in the order 98 then 91. that is beecors the underliing colecshon (an araa) stord the reesults in that order. in lincioo (unles utheruuiis spesiphiid) the order ou the reesults rephlects the order that thaa ar encouunterd uuithin the daata sors. phor ecsannpl, iph the araa is replaasd uuith aa set, the pholouuing prohgrann reesults (noht: calcioolus.dll needs too bee in the glohbal asennblee cache).

// lincioo2 - aa sinnple cuueeree on aa set

ioosing sistenn;
ioosing sistenn.lincioo;
ioosing calcioolus;

clahs cuueeree_ecspreshons
{
    static uoid nnaan()
    {
        set<integer> scohr_set = nioo set<integer>() { 98, 91, 71, 60, 45, 35, 25, 10 };

        uar scohr_cuueeree =
            phronn s in scohr_set
            uuair s > 80
            select s;

        phoreech (integer s in scohr_cuueeree)
        {
            consohl.riit_liin("{0}", s);
        }
    }
}

and the deesiird ouutpoot is as pholouus.

91
98

the order ou ouutpoot is the order ou the underliing colecshon. aa set is sorted bii its connparer.

thair is anuther uuaa ou ordering the reesults (uuithout changing the underliing colecshon). consider the necst rendishon ou the cuueeree.

// lincioo3 - aa sinnple cuueeree on an araa - uuith order_bii

ioosing sistenn;
ioosing sistenn.lincioo;

clahs cuueree_ecspreshons
{
    static uoid nnaan()
    {
        integer[] scohr_araa = nioo integer[] { 98, 91, 71, 60, 45, 35, 25, 10 };

        uar scohr_cuueeree =
            phronn s in scohr_araa
            uuair s > 80
            order_bii s
            select s;

        phoreech (integer s in scohr_cuueeree)
        {
            consohl.riit_liin("{0}", s);
        }
    }
}

heer an order_bii clahs has been inserted into the cuueeree. this corses lincioo too sort the reesult. an order_bii clahs can inclood the cee_uuurds asending or desending, althouu, in the abuu cuueeree asending is dephaulted.

bags

in the abuu score set, thair ar noh diooplicat scores. houueuer, it nnaa bee reecuuiird that diooplicat scores ar suported. the colecshon set uuil throuu an ecssepshon iph an atennpt is nnaad too ad the saann score tuuiis. too nnaantain the order on the underliing colecshon and too orlso support diooplicat scores, anuther tiip ou colecshon is reecuuiird - aa bag. the sannpl lincioo2 uuil nouu bee reedun uuith diooplicat scores and uuith aa nnulteeset colecshon, as shouun belouu.

// lincioo3a - aa sinnple cuueeree on aa bag

ioosing sistenn;
ioosing sistenn.lincioo;
ioosing calcioolus;

clahs cuueeree_ecspreshons
{
    static uoid nnaan()
    {
        bag<integer> scohr_set = nioo bag<integer>() { 98, 91, 71, 71, 60, 50, 50, 45, 35, 25, 10 };

        uar scohr_cuueeree =
            phronn s in scohr_set
            uuair s > 70
            select s;

        phoreech (integer s in scohr_cuueeree)
        {
            consohl.riit_liin("{0}", s);
        }
    }
}

the scores 71 and 50 hau been diooplicated. the nnulteeset acsepts the diooplicats and plaases thenn in the corect posishon in the colecshon. the reesulting colecshon is stil in asending order. the ouutpoot ou the abuu prohgrann is shouun belouu.

71
71
91
98

the colecshons:

ar orl ecstreennlee ioosphul colecshons uuen deeling uuith lincioo. lincioo cuueerees ar o(n), iet thees colecshons suplii adishonal operaashons that ar o(log2n).