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)