LINQ to SQL statement union/intersect/except

Source: Internet
Author: User
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=" Asi              A ", 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=" Mo  Scow ", 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 custome R {id= "J", city= "Lagos", country= "Nigeria", region= "Africa", sales=1000}, new Customer {id= "K", city= "Mexi CO City ", country=" Mexico ", region=" North America ", sales=2000}, new Customer {id=" L ", city=" Jakarta ", Coun Try= "Indonesia", region= "Asia", sales=3000}, new Customer {id= "M", city= "Tokyo", country= "Japan", region= "A Sia ", sales=4000}, new Customer {id=" N ", city=" Los Angeles ", country=" USA ", region=" North America ", sales=5 }, 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= "L Ondon ", 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 bet

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.
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.
Union (Consolidated)
Description: Connect different collections to automatically filter the same items; That is, merging two collections to filter the same items.

The above is the LINQ to SQL statement union/intersect/except content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.