程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> LINQ to SQL語句Union/Intersect/Except介紹

LINQ to SQL語句Union/Intersect/Except介紹

編輯:關於.NET
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="So 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();  
        }  
    }  
      
}

查看本欄目

輸入結果:

小注:

Intersect(相交)

說明:取相交項;延遲。即是獲取不同集合的相同項(交集)。即先遍歷第一個集合,找出所有唯一的元素,然後遍歷第二個集合,並將每個元素與前面找出的元素作對比,返回所有在兩個集合內都出現的元素。
Except(與非)

說明:排除相交項;延遲。即是從某集合中刪除與另一個集合中相同的項。先遍歷第一個集合,找出所有唯一的元素,然後再遍歷第二個集合,返回第二個集合中所有未出現在前面所得元素集合中的元素。
Union(合並)

說明:連接不同的集合,自動過濾相同項;延遲。即是將兩個集合進行合並操作,過濾相同的項。

作者:csdn博客 衣舞晨風

    No
  1. 下一頁:
Copyright © 程式師世界 All Rights Reserved