Concat/Union/Intersect/Except T and linqintersect of the linq to SQL statement (8)

Source: Internet
Author: User

Concat/Union/Intersect/Except T and linqintersect of the linq to SQL statement (8)

Applicable scenarios: process two sets, such as append, merge, take the same item, intersection item, and so on.

Concat (connection)

Note: different sets are connected, and the same items are not automatically filtered; latency.

1. Simple Form:
var q = (         from c in db.Customers         select c.Phone        ).Concat(         from c in db.Customers         select c.Fax        ).Concat(         from e in db.Employees         select e.HomePhone        );

Statement Description: return the telephone and fax of all consumers and employees.

2. Compound form:
var q = (         from c in db.Customers         select new         {             Name = c.CompanyName,             c.Phone         }        ).Concat(         from e in db.Employees         select new         {             Name = e.FirstName + " " + e.LastName,             Phone = e.HomePhone         }        );

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

Union)

Note: different sets are connected to automatically filter the same items; latency. Merge the two sets to filter the same items.

var q = (         from c in db.Customers         select c.Country        ).Union(         from e in db.Employees         select e.Country        );

Statement Description: query the country in which the customer and employee are located.

Intersect (intersection)

Description: obtains the intersection items and delays. That is, the same item (intersection) of different sets is obtained ). That is, first traverse the first set, find all unique elements, then traverse the second set, and compare each element with the elements found above, returns all elements that appear in both sets.

var q = (         from c in db.Customers         select c.Country        ).Intersect(         from e in db.Employees         select e.Country        );

Statement Description: query the countries in which customers and employees are located.

Except (and non)

Note: intersection items are excluded; delay. That is, to delete the same items from a collection and from another collection. First, traverse the first set, find all unique elements, and then traverse the second set. Then, return all elements in the second set that are not present in the previous element set.

var q = (         from c in db.Customers         select c.Country        ).Except(         from e in db.Employees         select e.Country        );

Statement Description: Query countries in which customers and employees are located.

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.