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.