LINQ in action Reading Notes: Using join 1, Group join 2, inner join 3, left Outer Join 4, cross join use and Difference

Source: Internet
Author: User

data source type used:

Static public class sampledata {static public publisher [] publishers = {new publisher {name = "funbooks"}, new publisher {name = "Joe publishing "}, new publisher {name = "I publisher" }}; static public author [] authors = {new author {firstname = "Johnny", lastname = "good "}, new author {firstname = "Graziella", lastname = "simplegame"}, new author {firstname = "Octavio", lastname = "Prince "}, new author {firstname = "Jeremy", lastname = "Legrand" }}; static public subject [] subjects = {New Subject {name = "Software Development "}, new subject {name = "novel"}, new subject {name = "Science Fiction" }}; static public book [] books = {new book {Title = "funny stories ", publisher = Publishers [0], authors = new [] {authors [0], authors [1]}, pagecount = 101, price = 25.55 M, publicationdate = new datetime (2004, 11, 10), ISBN = "0-000-77777-2", subject = subjects [0]}, new book {Title = "LINQ rules ", publisher = Publishers [1], authors = new [] {authors [2]}, pagecount = 300, price = 12 m, publicationdate = new datetime (2007, 9, 2 ), ISBN = "0-111-77777-2", subject = subjects [0]}, new book {Title = "C # on Rails", publisher = Publishers [1], authors = new [] {authors [2]}, pagecount = 256, price = 35.5 M, publicationdate = new datetime (2007, 4, 1 ), ISBN = "0-222-77777-2", subject = subjects [0]}, new book {Title = "all your base are belong to us ", publisher = Publishers [1], authors = new [] {authors [3]}, pagecount = 1205, price = 35.5 M, publicationdate = new datetime (2006, 5, 5 ), ISBN = "0-333-77777-2", subject = subjects [2]}, new book {Title = "Bonjour mon amour", publisher = Publishers [0], authors = new [] {authors [1], authors [0]}, pagecount = 50, price = 29 m, publicationdate = new datetime (1973, 2, 18 ), ISBN = "2-444-77777-2", subject = subjects [1] }};}

1. Group connection

Group connections are the same as group queries. The result is obtained by grouping. The following is an example. The result is obtained based on the publisther group.

The query statement for using group connection is as follows:

 
Gridviewgroupjoin. datasource = from publisher in sampledata. publishers join book in sampledata. books on publisher equals book. publisher into publisherbooks select new {publisher = publisher. name, books = publisherbooks}; gridviewgroupjoin. databind ();

AboveCodeIs the so-called "group connection ". It groups the books of each publishing house into publisherbooks and binds them together. This code is consistent with the running result of the following code.

 
VaR x = from book in sampledata. Books Group book by book. Publisher into bookpublishers select new {publisher = bookpublishers. Key. Name, books = bookpublishers };

2. Internal Connection

The inner join is the same as the inner join in SQL, that is, the intersection of the two sequences is found. See the following example to find out the data that the publisher in the book exists in sampledata. Publishers.

The internal connection query statement is as follows:

 
Gridviewinnerjoin. datasource = from publisher in sampledata. publishers join book in sampledata. books on publisher equals book. publisher select new {publisher = publisher. name, Book = book. title}; gridviewinnerjoin. databind ();

The inner join is used to locate the intersection of two sequences. After the inner join operation, elements from the two sequences that meet the specified conditions will be combined to form a new sequence. The join operator implements inner join based on key matching in the element.

3. left Outer Join

In the inner join, only a combination that meets the conditions in the two to be connected sequences will appear in the result sequence. However, if we need to retain all elements in the external sequence, and whether or not it has corresponding and qualified internal sequence elements, we need to executeLeft Outer JoinOperation.

Gridviewleftouterjoin. datasource = from publisher in sampledata. publishers join book in sampledata. books on publisher equals book. publisher into publisherbooks from book in publisherbooks. defaultifempty () Select New {publisher = publisher. name, Book = book = default (book )? "(No books)": Book. Title}; gridviewleftouterjoin. databind ();

Defaultifempty () can provide a default element for an empty sequence. Defaultifempty uses the generic default keyword. For the reference type, null is returned, and 0 is returned for the value type. For struct types, their corresponding initialization values are null or 0 based on their member types.

4. Cross join

A crossover is used to calculate the Cartesian product of all elements in two sequences. The result sequence consists of a complete combination of each element in a sequence and each element in another sequence. The number of elements in the result sequence is the product of the number of elements in the two source sequences.

Cross join is not implemented through the join operator in LINQ. In the terms of LINQ, cross join refers to a type of projection operation, which can be implemented through the selectmany operator or using multiple from clauses in the query expression.

Gridviewcrossjoin. datasource = from publisher in sampledata. publishers from book in sampledata. books select new {correct = publisher = book. publisher, publisher = publisher. name, Book = book. title}; gridviewcrossjoin. databind ();

Selectmany operator mode:

 
Sampledata. Publishers. selectmany (publisher => sampledata. Books. Select (Book => New {publishername = publisher. Name, bookname = book. Title }));

 

LINQ in action Reading Notes.

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.