21.c# sequence over-filtration, ordering, let clauses, and joins (Chapter 11 11.3-11.5)

Source: Internet
Author: User

Haha, after one weeks, how to send an article, to read more books Ah, the book can not stop ~ ~ ~

    • Using a WHERE clause to make a worry

The syntax format for the WHERE clause is as follows: where the expression

Example: Create a new jewelry class, as follows:

1 classJewellery2 {3     /// <summary>4     ///Jewelry Type5     /// <list type= "Ring" >rings</list>6     /// <list type= "Necklace" >Necklace</list>7     /// <list type= "Bracelet" >Bracelets</list>8     /// </summary>9      Public enumJewellerytypeTen     { One Ring, A Necklace, - Bracelet -     } the     /// <summary> -     ///Current Jewelry Status -     /// <list type= "Stock" >Stock</list> -     /// <list type= "Repair" >in Repair</list> +     /// <list type= "Sold" >has been sold</list> -     /// </summary> +      Public enumjewellerystate A     { at Stock, - Repair, - Sold -     } -      PublicJewellerytype Type {Get;Set; } -      Public DoublePrice {Get;Set; } in      PublicJewellerystate State {Get;Set; } -}

Then we use a list to make the container, through LINQ.

1list<jewellery> list =NewList<jewellery>()2 {3     NewJewellery () {type=jewellery.jewellerytype.bracelet,state=jewellery.jewellerystate.repair,price= - },4     NewJewellery () {type=jewellery.jewellerytype.necklace,state=jewellery.jewellerystate.sold,price= $ },5     NewJewellery () {type=jewellery.jewellerytype.ring,state=jewellery.jewellerystate.stock,price= - },6     NewJewellery () {type=jewellery.jewellerytype.necklace,state=jewellery.jewellerystate.sold,price= - },7     NewJewellery () {type=jewellery.jewellerytype.bracelet,state=jewellery.jewellerystate.stock,price= - },8     NewJewellery () {type=jewellery.jewellerytype.ring,state=jewellery.jewellerystate.repair,price= - }9 };Ten  One //find a bracelet with a price greater than 300 A varresult = fromIteminchList -              whereItem. Price > -&& item. Type = =Jewellery.JewelleryType.Bracelet -              Selectitem; the //Convert to list. Where (item = Item. Price > 300). Where (item = Item. Type = = Jewellery.JewelleryType.Bracelet); - Console.WriteLine (Result. Count ()); -  - //record the total value of the jewels currently being repaired + varRESULT1 = ( fromIteminchList -                whereItem. state = =Jewellery.JewelleryState.Repair +                SelectItem). Sum (item =item. Price); A //Convert to list. Where (item = Item. state = = Jewellery.JewelleryState.Repair). Sum (item = Item. Price); at Console.WriteLine (RESULT1); -  -Console.readkey ();
    • Degenerate query expressions
1 var  from inch List 2               Select item; 3 // This is called a degenerate expression, and the compiler intentionally generates a call to the Select method, even if it does nothing, but the RESULT2 and list are very different, 4 // The data returned by two sequences is the same, but the result of the Select method is only a sequence of data items, not the data source itself. The result of the query expression and the source data will never be the same object 5// when there are other operations, you do not have to leave a "null operation" SELECT clause for the compiler
    • Sort using the ORDER BY clause
 1  //  2  var  resutl3 = from  Item in   list  3  orderby   item. Price descending  4  select  
    
      item; 
     5   Convert to list. OrderByDescending (item = Item). Price);  
1 // Sort by price and then by status first 2 var  from inch List 3                by item. Price, item. State4               select item;
    • Let clause

The LET clause simply introduces a new scope variable whose value is based on the other range variable, syntax format: let identifier = expression

1 // the goods are in RMB, now recorded using HK, using projection to return the sequence of anonymous objects 2 var  from inch List 3               0.8 4               Select New {HK = HK, item = Item};

The Let operator evaluates an expression and introduces a new range variable

    • Connection

Corresponding to the concept in the database, using two tables, here is the use of two sequences, by matching the data row between the two to create the results.

    1. Using inner joins of join clauses

An inner connection involves two sequences. A key selector expression applies to each element of the 1th sequence, and the other key selector (which may be completely different) applies to each element of the second sequence. The result of the connection is a sequence that contains all the paired elements, and the rule is that the key of the 1th element is the same as the key of the 2nd element.

Concatenated format: Join right sequence element in right sequence

On left sequence element key equals right sequence element key

1 /// <summary>2 ///a box for storing jewelry3 /// </summary>4 classBox5 {6     /// <summary>7     ///The box has a property of a jewelry type, consistent with the type of jewelry to be placed8     /// </summary>9      PublicJewellery.jewellerytype Jewellerytype {Get;Set; }Ten      Public stringBoxname {Get;Set; } One}
1 //print the boxes for each jewel, the status of the jewels if they are in stock2 varRESULT6 = fromBoxinchboxlist3Join iteminchList4 On box.jewellerytype equals item. Type5               whereItem. state = =Jewellery.JewelleryState.Stock6               Select New{boxname = box. Boxname, Price =item. Price};7 8Result6. ToList (). ForEach (item = Console.WriteLine ("Location:"+ Item. Boxname +";"+"Price:"+ Item. Price));

The results are as follows

A jewel with a price of 500 was placed in the Box1 and 300 jewels were placed in the box3. Here as the key of the two series is the type of jewelry Jewellery.jewellerytype

Please treatise.

21.c# sequence over-filtration, ordering, let clauses, and joins (Chapter 11 11.3-11.5)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.