21. C # sequence consideration, sorting, let clause, and join (Chapter 11, 11.3-11.5 ),

Source: Internet
Author: User

21. C # sequence consideration, sorting, let clause, and join (Chapter 11, 11.3-11.5 ),

Haha, after a week, I have to publish an article about how to read more books ~~~

  • Use the where clause for consideration

The syntax format of the where clause is as follows: where excessive expressions

Example: Create a new jewelry class as follows:

1 class Jewellery 2 {3 /// <summary> 4 // jewelry type 5 /// <list type = "Ring"> Ring </list> 6 /// <list type = "neckmtr"> Necklace </list> 7 /// <list type = "Bracelet"> Bracelet </list> 8 /// </summary> 9 public enum jewelleryType10 {11 Ring, 12 neck.pdf, 13 Bracelet14} 15 // <summary> 16 // current jewelry status 17 /// <list type = "Stock"> Inventory </list> 18 // <list type = "Repair"> under Repair </list> 19 // <list type = "Sold"> Sold </list> 20 /// </summary> 21 public enum JewelleryState22 {23 Stock, 24 Repair, 25 Sold26} 27 public JewelleryType Type {get; set;} 28 public double Price {get; set;} 29 public JewelleryState State {get; set;} 30}

Then we use a List as the container, and use LINQ to worry about it.

1 List <Jewellery> list = new List <Jewellery> () 2 {3 new Jewellery () {Type = Jewellery. jewelleryType. bracelet, State = Jewellery. jewelleryState. repair, Price = 100}, 4 new Jewellery () {Type = Jewellery. jewelleryType. neckery, State = Jewellery. jewelleryState. sold, Price = 200}, 5 new Jewellery () {Type = Jewellery. jewelleryType. ring, State = Jewellery. jewelleryState. stock, Price = 300}, 6 new Jewellery () {Type = Jewellery. jewelleryType. neckery, State = Jewellery. jewelleryState. sold, Price = 400}, 7 new Jewellery () {Type = Jewellery. jewelleryType. bracelet, State = Jewellery. jewelleryState. stock, Price = 500}, 8 new Jewellery () {Type = Jewellery. jewelleryType. ring, State = Jewellery. jewelleryState. repair, Price = 600} 9}; 10 11 // find the bracelet with a Price greater than 300 12 var result = from item in list13 where item. price> 300 & item. type = Jewellery. jewelleryType. bracelet14 select item; 15 // convert to list. where (item => item. price> 300 ). where (item => item. type = Jewellery. jewelleryType. bracelet); 16 Console. writeLine (result. count (); 17 18 // record total jewelry value currently under repair 19 var result1 = (from item in list20 where item. state = Jewellery. jewelleryState. repair21 select item ). sum (item => item. price); 22 // convert to list. where (item => item. state = Jewellery. jewelleryState. repair ). sum (item => item. price); 23 Console. writeLine (result1); 24 25 Console. readKey ();
  • Degraded query expression
1 var resutl2 = from item in list2 select item; 3 // This is a so-called degraded expression. The compiler will intentionally generate a call to the Select method, even if it does nothing, however, result2 and list are quite different. 4 // The data returned by the 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 query expression result and source data will never be the same object. // when there are other operations, you do not need to reserve a "null operation" Select clause for the compiler.
  • Sort by using the orderby clause
1 // sort by price, from large to small 2 var resutl3 = from item in list3 orderby item. price descending4 select item; 5 // convert to list. orderByDescending (item => item. price );
1 // first use the Price and then sort by status 2 var result4 = from item in list3 orderby item. Price, item. State4 select item;
  • Let clause

The let clause only introduces a new range variable. Its value is based on other range variables. Syntax format: let identifier = expression

1 // The goods are stored in RMB. Now we use HK to record and use projection to return the sequence of anonymous objects 2 var result5 = from item in list3 let hk = item. price/0.84 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, two tables are used. Here two sequences are used to create results by matching the data rows between the two.

The inner join involves two sequences. One key selector expression is applied to every element of 1st sequences, and the other key selector (which may be completely different) is applied to every element of the second sequence. The join result is a sequence containing all the paired elements. The pairing rule is that the keys of the 1st elements are the same as those of the 2nd elements.

Join format: join right sequence element in Right Sequence

On

1 /// <summary> 2 /// Box for storing jewelry 3 /// </summary> 4 class Box 5 {6 /// <summary> 7 /// the box has a property of the jewelry type, consistent with the jewelry type to be placed 8 /// </summary> 9 public Jewellery. jewelleryType jewelleryType {get; set;} 10 public string BoxName {get; set;} 11}
1 // print the box where each jewelry is stored. The Jewelry status is inventory 2 var result6 = from box in boxList3 join item in list4 on box. jewelleryType equals item. type5 where item. state = Jewellery. jewelleryState. stock6 select new {BoxName = box. boxName, Price = item. price}; 7 8 result6.ToList (). forEach (item => Console. writeLine ("Location:" + item. boxName + ";" + "Price:" + item. price ));

The result is as follows:

It can be seen that a 500-price jewelry is placed in box1, and a 300-price jewelry is placed in box3. The keys of the two sequences here are the jewelry type Jewellery. JewelleryType.

Please make an axe.

 

Related Article

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.