LINQ to SQL语句 Union/Intersect/Except
LINQ to SQL statement Union/Intersect/Except
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BegVCSharp_23_15_SetOperators { class Customer { public string ID { get; set; } public string City { get; set; } public string Country { get; set; } public string Region { get; set; } public decimal Sales { get; set; } } class Order { public string ID { get; set; } public decimal Amount { get; set; } } class Program { static void Main(string[] args) { List<Order> orders = new List<Order> { new Order { ID="P", Amount=100 }, new Order { ID="Q", Amount=200 }, new Order { ID="R", Amount=300 }, new Order { ID="S", Amount=400 }, new Order { ID="T", Amount=500 }, new Order { ID="U", Amount=600 }, new Order { ID="V", Amount=700 }, new Order { ID="W", Amount=800 }, new Order { ID="X", Amount=900 }, new Order { ID="Y", Amount=1000 }, new Order { ID="Z", Amount=1100 } }; List<Customer> customers = new List<Customer> { new Customer { ID="A", City="New York", Country="USA", Region="North America", Sales=9999}, new Customer { ID="B", City="Mumbai", Country="India", Region="Asia", Sales=8888}, new Customer { ID="C", City="Karachi", Country="Pakistan", Region="Asia", Sales=7777}, new Customer { ID="D", City="Delhi", Country="India", Region="Asia", Sales=6666}, new Customer { ID="E", City="São Paulo", Country="Brazil", Region="South America", Sales=5555 }, new Customer { ID="F", City="Moscow", Country="Russia", Region="Europe", Sales=4444 }, new Customer { ID="G", City="Seoul", Country="Korea", Region="Asia", Sales=3333 }, new Customer { ID="H", City="Istanbul", Country="Turkey", Region="Asia", Sales=2222 }, new Customer { ID="I", City="Shanghai", Country="China", Region="Asia", Sales=1111 }, new Customer { ID="J", City="Lagos", Country="Nigeria", Region="Africa", Sales=1000 }, new Customer { ID="K", City="Mexico City", Country="Mexico", Region="North America", Sales=2000 }, new Customer { ID="L", City="Jakarta", Country="Indonesia", Region="Asia", Sales=3000 }, new Customer { ID="M", City="Tokyo", Country="Japan", Region="Asia", Sales=4000 }, new Customer { ID="N", City="Los Angeles", Country="USA", Region="North America", Sales=5000 }, new Customer { ID="O", City="Cairo", Country="Egypt", Region="Africa", Sales=6000 }, new Customer { ID="P", City="Tehran", Country="Iran", Region="Asia", Sales=7000 }, new Customer { ID="Q", City="London", Country="UK", Region="Europe", Sales=8000 }, new Customer { ID="R", City="Beijing", Country="China", Region="Asia", Sales=9000 }, new Customer { ID="S", City="Bogotá", Country="Colombia", Region="South America", Sales=1001 }, new Customer { ID="T", City="Lima", Country="Peru", Region="South America", Sales=2002 } }; var customerIDs = from c in customers select c.ID ; var orderIDs = from o in orders select o.ID ; var customersWithOrders = customerIDs.Intersect(orderIDs); Console.WriteLine("Customers with Orders:"); foreach (var item in customersWithOrders) { Console.Write("{0} ", item); } Console.WriteLine(); Console.WriteLine("Orders with no customers:"); var ordersNoCustomers = orderIDs.Except(customerIDs); foreach (var item in ordersNoCustomers) { Console.Write("{0} ", item); } Console.WriteLine(); Console.WriteLine("All Customer and Order IDs:"); var allCustomerOrderIDs = orderIDs.Union(customerIDs); foreach (var item in allCustomerOrderIDs) { Console.Write("{0} ", item); } Console.WriteLine(); Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); } } }
Input result:
Small note:
Intersect
Description: Get the intersection item; delay. That is, getting the same items (intersection) from different collections. That is, first traverse the first set to find all unique elements, then traverse the second set, compare each element with the previously found element, and return all elements that appear in both sets.
Except (AND NOT)
Description: Exclude intersecting items; delay. That is, delete items from one collection that are the same as those from another collection. First traverse the first set to find all unique elements, then traverse the second set and return all elements in the second set that do not appear in the previously obtained set of elements.
Union (merge)
Description: Connect different collections and automatically filter the same items; delay. That is, the two collections are merged and identical items are filtered.
The above is the content of LINQ to SQL statement Union/Intersect/Except. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to create tables using SQL statements in SQL Server: Open SQL Server Management Studio and connect to the database server. Select the database to create the table. Enter the CREATE TABLE statement to specify the table name, column name, data type, and constraints. Click the Execute button to create the table.

This article introduces a detailed tutorial on joining three tables using SQL statements to guide readers step by step how to effectively correlate data in different tables. With examples and detailed syntax explanations, this article will help you master the joining techniques of tables in SQL, so that you can efficiently retrieve associated information from the database.

Methods to judge SQL injection include: detecting suspicious input, viewing original SQL statements, using detection tools, viewing database logs, and performing penetration testing. After the injection is detected, take measures to patch vulnerabilities, verify patches, monitor regularly, and improve developer awareness.

The SQL INSERT statement is used to insert data into a table. The steps include: specify the target table to list the columns to be inserted. Specify the value to be inserted (the order of values must correspond to the column name)

The methods to check SQL statements are: Syntax checking: Use the SQL editor or IDE. Logical check: Verify table name, column name, condition, and data type. Performance Check: Use EXPLAIN or ANALYZE to check indexes and optimize queries. Other checks: Check variables, permissions, and test queries.

phpMyAdmin can be used to create databases in PHP projects. The specific steps are as follows: Log in to phpMyAdmin and click the "New" button. Enter the name of the database you want to create, and note that it complies with the MySQL naming rules. Set character sets, such as UTF-8, to avoid garbled problems.

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

Creating an Oracle database is not easy, you need to understand the underlying mechanism. 1. You need to understand the concepts of database and Oracle DBMS; 2. Master the core concepts such as SID, CDB (container database), PDB (pluggable database); 3. Use SQL*Plus to create CDB, and then create PDB, you need to specify parameters such as size, number of data files, and paths; 4. Advanced applications need to adjust the character set, memory and other parameters, and perform performance tuning; 5. Pay attention to disk space, permissions and parameter settings, and continuously monitor and optimize database performance. Only by mastering it skillfully requires continuous practice can you truly understand the creation and management of Oracle databases.
