Concat/union/intersect/except of LINQ to SQL statements (8)

Source: Internet
Author: User

Applicable scenario: Handling of two collections, such as append, merge, take same item, intersect item, and so on.

Concat (Connection)

Note: Connecting different collections does not automatically filter the same items;

1. Simple form:
var q = (         from in  db. Customers         Select  c.phone        ). Concat (         from in  db. Customers         Select  c.fax        ). Concat (         from in  db. Employees         Select  e.homephone        );

Statement description: Returns the phone and fax for all consumers and employees.

2. Composite form:
varQ = (          fromCinchdb. CustomersSelect New{Name=c.companyname, C.phone}). Concat ( fromEinchdb. EmployeesSelect New{Name= E.firstname +" "+E.lastname, Phone=e.homephone});

Statement Description: Returns the name and phone number of all consumers and employees.

Union (Consolidated)

Description: Connect different collections to automatically filter the same items; That is, merging two collections to filter the same items.

var q = (         from in  db. Customers         Select  c.country        ). Union (         from in  db. Employees         Select  e.country        );

Statement Description: Query the country where the customer and the employee are located.

Intersect (intersect)

Description: Take intersect item; delay. That is, get the same item (intersection) of different sets. That is, it iterates through the first collection, finds all the unique elements, then iterates through the second set and compares each element to the element found earlier, returning all elements that appear within the two collection.

var q = (         from in  db. Customers         Select  c.country        ). Intersect (         from in  db. Employees         Select  e.country        );

Statement Description: Query the country where the customer and the employee are in.

Except (with non)

Description: Exclude intersecting items; That is, you remove the same item from a collection as in another collection. Iterates through the first collection, finds all the unique elements, and then iterates through the second collection, returning all the elements in the second collection that do not appear in the preceding collection of elements.

var q = (         from in  db. Customers         Select  c.country        ). Except (         from in  db. Employees         Select  e.country        );

Statement Description: Query the customer and staff of different countries.

Concat/union/intersect/except of LINQ to SQL statements (8)

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.