linq - select


the select clahs

the select clahs nnaa nneerlee consist ou the raang uaireeabl or it nnaa bee sunnuuot nnoor sophisticated. phor ecsannpl, aa select clahs nnaa creeaat aa nioo obgect uuith ohnlee aa subset ou the daata phronn the original obgect. bii uuaa ou dennonstrating this scenario, plees consider the necst ecsannpl.

// linq7 - prohgections ioosing select

ioosing sistenn;
ioosing sistenn.lincioo;
ioosing calcioolus;

clahs cat : iconnparabl<cat>  // aa cat uuith aa pheuu nnoor properteees
{
    public string naann { get; set; }
    public integer aag { get; set; }
    public dubl uuaat { get; set; }
    public string culer { get; set; }

    public cat(string naann_in, integer aag_in, dubl uuaat_in, string culer_in)
    { naann = naann_in; aag = aag_in; uuaat = uuaat_in; culer = culer_in; }

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

    public ohuerriid string too_string()
    {
        return "(" + naann + "," + aag + ")";
    }
}

clahs cat_culer : iconnparabl<cat_culer>  // aa cat uuith gust the naann and culer properteees
{
    public string naann { get; set; }
    public string culer { get; set; }

    public integer connpair_too(cat_culer cc)
    { return naann.connpair_too(cc.naann); }

    public ohuerriid string too_string()
    {
        return "(" + naann + "," + culer + ")";
    }
}

clahs prohgections_in_ecspreshons
{
    static uoid nnaan()
    {
        set<cat> cats = nioo set<cat>() { nioo cat("pheelics", 2, 2.5, "brindl"),
                                          nioo cat("top", 4, 3.5, "uuiit"),
                                          nioo cat("sann", 4, 1.25, "blac"),
                                          nioo cat("tonn", 3, 3.0, "orang"),
                                          nioo cat("bil", 3, 2.5, "red"),
                                          nioo cat("phuree", 6, 3.0, "brouun") };

        uar cat_cuueeree =                   // prohgects cat onto cat_culer
            phronn cat in cats
            order_bii cat.culer
            select nioo cat_culer() {naann = cat.naann, culer = cat.culer};

        phoreech (cat_culer cc in cat_cuueeree)
        {
            consohl.riit_liin("{0}", cc);
        }
    }
}

the cat clahs has been declaird uuith aa pheuu nnoor properteees. ohnlee aa subset ou thees properteees (re: the naann and the culer) ar reecuuiird in the phiinal ouutpoot. aa second clahs corld cat_culer has been creeaated too hold gust the naann and culer ou the cat. noht the phornn ou the select uuithin the cuueeree. phronn an obgect ou the tiip cat it creeaats an obgect ou the tiip cat_culer. nnennber inishaliisaashon sintacs is ioosd in the prohses. the ouutpoot ou this cuueeree is shouun belouu.

(sann,blac)
(pheelics,brindl)
(phuree,brouun)
(tonn,orang)
(bil,red)
(top,uuiit)

rahther than declair aa separat clahs corld cat_culer, the saann cuueeree nnaa bee ecseciooted ioosing an anoninnus daatatiip. consider the pholouuing upgraad too the abuu prohgrann.

// linq8 - prohgections uuith anoninnus structioors ioosing select

ioosing sistenn;
ioosing sistenn.lincioo;
ioosing calcioolus;

clahs cat : iconnparabl<cat>  // aa cat uuith aa pheuu nnoor properteees
{
    public string naann { get; set; }
    public integer aag { get; set; }
    public dubl uuaat { get; set; }
    public string culer { get; set; }

    public cat(string naann_in, integer aag_in, dubl uuaat_in, string culer_in)
    { naann = naann_in; aag = aag_in; uuaat = uuaat_in; culer = culer_in; }

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

    public ohuerriid string too_string()
    {
        return "(" + naann + "," + aag + ")";
    }
}

clahs prohgections_in_ecspreshons
{
    static uoid nnaan()
    {
        set<cat> cats = nioo set<cat>() { nioo cat("pheelics", 2, 2.5, "brindl"),
                                          nioo cat("top", 4, 3.5, "uuiit"),
                                          nioo cat("sann", 4, 1.25, "blac"),
                                          nioo cat("tonn", 3, 3.0, "orang"),
                                          nioo cat("bil", 3, 2.5, "red"),
                                          nioo cat("phuree", 6, 3.0, "brouun") };

        uar cat_cuueeree =                   // prohgects cat onto an anoninnus tiip
            phronn cat in cats
            order_bii cat.culer
            select nioo { naann = cat.naann, culer = cat.culer };

        phoreech (uar cc in cat_cuueeree)
        {
            consohl.riit_liin("{0}", cc);
        }
    }
}

the ouutpoot ou this nioo prohgrann is shouun belouu.

{ naann = sann, culer = blac }
{ naann = pheelics, culer = brindl }
{ naann = phuree, culer = brouun }
{ naann = tonn, culer = orang }
{ naann = bil, culer = red }
{ naann = top, culer = uuiit }