22. C # grouping and querying continuation and selection syndrome (Chapter 11, 11.6-11.7 ),

Source: Internet
Author: User

22. C # grouping and querying continuation and selection syndrome (Chapter 11, 11.6-11.7 ),

There is another point of knowledge about yesterday's connection, that is, group connection. Is the content in 11.5. Add it.

Group join format: join element in sequence

On condition expression

Into new sequence

An important difference between an intranet connection and a group connection (that is, the difference between a group connection and a common group) is that for a group connection, there is a one-to-one relationship between the left sequence and the result sequence, even if some elements in the left sequence do not have any matching elements in the right sequence, it does not matter. This is very important and sometimes it is used to simulate the left Outer Join of SQL. When the element on the Left does not match any element on the right, the embedding sequence is empty. Like the internal connection, the group connection caches the right sequence and processes the stream of the Left sequence. Check the code.

1 // use grouping to connect 2 var result7 = from box in boxList 3 join item in list on box. jewelleryType equals item. type into items 4 select new {box, items}; 5 // The feeling of connecting to me by group is like packaging a series of jewelry (with the same property value, of course, a sequence is generated, while the box in the above 6 // statement serves as the identifier for storing these jewelry with the same attributes 7 Console. writeLine (result7.Count (); 8 9 foreach (var re in result7) 10 {11 Console. writeLine (re. box. boxName); 12 foreach (var item in re. items) 13 {14 Console. writeLine (item. price); 15} 16}

The code here is followed by yesterday's code. Haha, you may want to read the previous article.

  • Use multiple from clauses for cross join and merge Sequences

The connections we have learned so far are equal connections-the elements in the left sequence must match the right sequence. The cross join does not perform any matching operation between sequences. It only lists all possibilities as much as possible. Create a class named Customer

1 class Customer2 {3     public string Name { get; set; }4 }
 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.Necklace,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.Necklace,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 List<Customer> ctrList = new List<Customer>()12 {13     new Customer() {Name="111" },14     new Customer() {Name="222" },15     new Customer() {Name="333" },16     new Customer() {Name="444" },17     new Customer() {Name="555" }18 };19 20 var result9 = from ctr in ctrList21               from jly in list22               select new { ctr, jly };23 Console.WriteLine(result9.Count());

The result is 30, because there are 6 pieces of jewelry, 5 customers, 5*6 = 30. It can be seen that the use of two from clauses may cross-join to return the sequences of all combinations.

  • Use the group... by clause for grouping

Format: group element by keyword

The grouping expression uses its key to determine how the sequence is grouped. The whole result is a sequence. Each element in the sequence itself is the sequence of elements after projection, and also has a Key attribute, that is, the Key used for grouping. Next we will perform an operation to group the jewelry type.

 1 var result10 = from jly in list 2                group jly by jly.Type; 3  4 Console.WriteLine(result10.Count()); 5  6 foreach (var jlys in result10) 7 { 8     Console.WriteLine(jlys.Key); 9     foreach(var jly in jlys)10     {11         Console.WriteLine(jly.Price);12     }13 }

The effect of using the join... into clause is the same, but using group... by is more understandable.

  • Query continuation

Query continuation provides a way to use the results of one query expression as the initial sequence of another query expression. It can be applied to group... by and select clauses. The syntax is the same for the two. You only need to use the context keyword into. to query the number of each type of jewelry, it can be expressed as follows.

1 var result11 = from jly in list2                group jly by jly.Type into jlys3                select new { Type = jlys.Key, Count = jlys.Count() };4 5 foreach(var item in result11)6 {7     Console.WriteLine(item.Type);8     Console.WriteLine(item.Count);9 }

Result:

Indicates that each type has two pieces of jewelry.

  • Use a LINQ expression or an extension method

There are several ideas about how to choose. Here we only talk about the relationship between the two.

Each query expression may be expressed using an extension method, but many LING operations do not have an equivalent query expression in C.

Please make sure you have a learning attitude to do something seriously.

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.