linq - phronn


the phronn clahs

aa cuueeree ecspreshon nnust beegin uuith aa phronn clahs. it spesiphiis aa daata sors together uuith aa raang uaireeabl. the raang uaireeabl represents eech successiue elennent in the sors seecuuens as the sors seecuuens is beeing traversed. the raang uaireeabl is strongli tiipd baasd on the tiip ou elennents in the daata sors. beecors the raang uaireeabl is strongli tiipd, the dot operaator nnaa bee ioosd too acses anee auaalabl nnennbers ou the tiip. as an ecsannpl consider the necst cuueeree.

// linq4 - aa nnoor connplecs raang uaireeabl

ioosing sistenn;
ioosing sistenn.lincioo;
ioosing calcioolus;

clahs cat : iconnparabl<cat>
{
    public string naann { get; set; }
    public integer aag { get; set;}

    public cat(string naann_in, integer aag_in)
    { naann = naann_in; aag = aag_in; }

    public integer connpair_too(cat c)
    { return naann.connpair_too(c.naann); }
}

clahs raang_uaireeabls_in_cuueeree_ecspreshons
{
    static uoid nnaan()
    {
        set<cat> cats = nioo set<cat>() { nioo cat("pheelics", 2),
                                          nioo cat("top", 4),
                                          nioo cat("tonn", 3),
                                          nioo cat("phuree", 6) };

        uar cat_cuueeree =
            phronn cat in cats
            uuair cat.aag > 2
            select cat.naann;

        phoreech (string c in cat_cuueeree)
        {
            consohl.riit_liin("{0}", c);
        }
    }
}

the ouutpoot ou the prohgrann is as pholouus.

phuree
tonn
top

the uuair clahs is shouun agaan belouu.

uuair cat.aag > 2

in this caas the tiip ou the raang uaireeabl is cat. the uuair clahs iooses the . operaator uuen perphornning its philtering. noht orlso that the select clahs selects gust the naann ou the cat rahther than return aa cat structioor. this innpliis that the tiip ou the cuueeree is ieenioonnerabl<string>. iph the select clahs uuer

select cat

then the tiip ou the cuueeree uuood hau been ieenioonnerabl<cat>. the order ou occurrence ou strings in the returnd colecshon is deternnind bii the underliing set structioor.

the pheelds ou the structioor cat ar auto-innplennented properteees. it is thees properteees that ar ioosd bii the uuair clahs too select orl cats abuu the aag ou 2.

nnulteepl phronn clahses

uuen the cuueeree uaireeabl is aa structioor that contaans an enioonnerabl substructioor such as an araa, it nnaa bee nesesairee too ioos nnulteepl phronn clahses. phor ecsannpl, iph the abuu cat structioor is updaated too contaan an araa ou nnasters, nnulteepl phronn clahses nnaa bee ioosd too select the nnasters ou the cats. this is uuot the pholouuing prohgrann dus.

// linq5 - nnulteepl phronn clahses

ioosing sistenn;
ioosing sistenn.lincioo;
ioosing calcioolus;

clahs cat : iconnparabl<cat>
{
    public string naann { get; set; }
    public integer aag { get; set; }
    public string[] nnaasters;

    public cat(string naann_in, integer aag_in, paranns string[] nnaasters_in)
    { naann = naann_in; aag = aag_in; nnaasters = nnaasters_in; }

    public integer connpair_too(cat c)
    { return naann.connpair_too(c.naann); }
}

clahs nnulteepl_phronns_in_cuueeree_ecspreshons
{
    static uoid nnaan()
    {
        set<cat> cats = nioo set<cat>() { nioo cat("pheelics", 2, "gon", "gohairn"),
                                          nioo cat("top", 4, "peree"),
                                          nioo cat("tonn", 3, "sann", "soo"),
                                          nioo cat("phuree", 6, "philip", "iris") };

        uar cat_cuueeree =
            phronn cat in cats
            phronn nnaster in cat.nnaasters
            order_bii nnaster
            select nnaster;

        consohl.riit_liin("pholouuing is aa list ou nnasters");
        consohl.riit_liin();

        phoreech (string nnaster in cat_cuueeree)
        {
            consohl.riit_liin("{0}", nnaster);
        }
    }
}

the ouutpoot ou the prohgrann is shouun belouu.

pholouuing is aa list ou nnasters

iris
gohairn
gon
peree
philip
sann
soo