For yesterday's connection there is also a knowledge point is not said, that is the group connection. Is 11.5 of the content, fill it up.
Format of grouped joins: join element in sequence
On-Conditional expressions
into new sequence
An important difference between an inner join and a grouped connection (that is, the difference between a packet join and a normal grouping) is that for a grouped connection, there is a one-to-a correspondence 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. This is very important and is sometimes used to simulate the left outer join of SQL. The embedded sequence is empty when the left element does not match any of the right elements. Like an inner join, a grouped connection buffers the right sequence and streams the left sequence. Look at the code.
1 //using a grouped connection2 varRESULT7 = fromBoxinchboxlist3Join iteminchlist on Box.jewellerytype equals item. Type into Items4 Select New{box, items};5 //using a grouped connection I feel like I'm packing a series of jewels (with the same attribute values), and of course generating a sequence, and6 //The box in the statement is used as a symbol for storing some of these same property jewels.7 Console.WriteLine (result7. Count ());8 9 foreach(varReinchresult7)Ten { One Console.WriteLine (re.box.BoxName); A foreach(varIteminchre.items) - { - Console.WriteLine (item. Price); the } -}
The code here is followed by yesterday's code, haha, may want to look at the previous article.
- Using multiple from clauses for cross-joins and merge sequences
So far, the connections we've learned are equal joins--the elements in the left sequence and the right sequence are matched. A cross-join does not perform any matching operations between the sequences, it lists all possible only as much as possible. We create a new class called Customer .
1 class Customer 2 {3 Public string Get Set ; } 4 }
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 OneList<customer> ctrlist =NewList<customer>() A { - NewCustomer () {name="111" }, - NewCustomer () {name="222" }, the NewCustomer () {name="333" }, - NewCustomer () {name="444" }, - NewCustomer () {name="555" } - }; + - varResult9 = fromCtrinchctrlist + fromJlyinchList A Select New{CTR, jly}; atConsole.WriteLine (Result9. Count ());
The result is 30, because there are 6 pieces of jewellery, and the customer has 5 people, 5*6=30. Visible, using two from clauses may cross-connect to return all combined sequences.
- Grouping using the group...by clause
Format: group element by keyword
Grouping expressions use their keys to determine how sequences are grouped. The entire result is a sequence in which each element in the sequence is itself a sequence of projected elements, and also has a key property, which is the key used for grouping. Here's an operation to group with the type of jewelry.
1 varResult10 = fromJlyinchList2 group Jly by Jly. Type;3 4 Console.WriteLine (result10. Count ());5 6 foreach(varJlysinchresult10)7 {8 Console.WriteLine (Jlys. Key);9 foreach(varJlyinchJlys)Ten { One Console.WriteLine (jly. Price); A } -}
The effect of feeling and using the Join...into clause is the same, but the use of group...by can be more understandable.
Query continuation provides a way to use the result 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 both, and only requires the context keyword into, which can be said to query the quantity of each type of jewelry.
1 varResult11 = fromJlyinchList2 group Jly by Jly. Type into Jlys3 Select New{Type = Jlys. Key, Count =Jlys. Count ()};4 5 foreach(varIteminchresult11)6 {7 Console.WriteLine (item. Type);8 Console.WriteLine (item. Count);9}
The result is:
show that each type has two pieces of jewellery.
- Using a LINQ expression or using an extension method
For how to choose, each heart has a few, here is only the relationship between the two.
Each query expression may be represented using an extension method, but many ling operations do not have an equivalent query expression in C #.
Please treatise, take a learning mentality to do something seriously.
22.c# Group and query continuation and Selection syndrome (Chapter 11 11.6-11.7)