class Order
{
public string ID { get; set; }
public decimal Amount { get; set; }
}
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; }
public override string ToString()
{
return "ID:" + ID + "City:" + City + "Country:" + Country +
"Region:" + Region + "Sales:" + Sales;
}
}
class Program
{
static void Main(string[] args)
{
List<Customer> customers = new List<Customer>{
new Customer{ID = "A",City = "New York", Country = "USA",
Region = "North American", Sales = 999},
new Customer{ID = "B",City = "Mumbai", Country = "India",
Region = "Asia", Sales = 888},
new Customer{ID = "C",City = "Karachi", Country = "Pakistan",
Region = "Asia", Sales = 777},
new Customer{ID = "D",City = "Delhi", Country = "India",
Region = "Asia", Sales = 666},
new Customer{ID = "E",City = "So Paul", Country = "Brazil",
Region = "South America", Sales = 555},
new Customer{ID = "F",City = "Moscow", Country = "Russia",
Region = "Europe", Sales = 444},
new Customer{ID = "G",City = "Seoul", Country = "Korea",
Region = "Asia", Sales = 333},
new Customer{ID = "H",City = "Istanbul", Country = "Turkey",
Region = "Asia", Sales = 222},
new Customer{ID = "I",City = "Shanghai", Country = "China",
Region = "Asia", Sales = 1111},
new Customer{ID = "J",City = "Lagos", Country = "Nigeria",
Region = "Africa", Sales = 1000}};
List<Order> orders = new List<Order>{
new Order{ID="E", Amount = 100},
new Order{ID="F", Amount = 200},
new Order{ID="G", Amount = 300},
new Order{ID="H", Amount = 400},
new Order{ID="I", Amount = 500},
new Order{ID="J", Amount = 600},
new Order{ID="K", Amount = 700},
new Order{ID="L", Amount = 800},
new Order{ID="M", Amount = 900},
new Order{ID="N", Amount = 1000},
new Order{ID="O", Amount = 1100}};
var customerIDs =
from c in customers
select c.ID;
var orderIDs =
from o in orders
select o.ID;
//-----------------------------------------Intersect、Excpt、Union用法 begin----------------------------------------------------
/* var customersWithOrders = customerIDs.Intersect(orderIDs);//两个集合的交集
foreach (var item in customersWithOrders)
{
Console.WriteLine(item);
}
var customersWithOrders = customerIDs.Except(orderIDs);//第一个结果集中有,而第二个结果集中没有的项
foreach (var item in customersWithOrders)
{
Console.WriteLine(item);
}
var customersWithOrders = customerIDs.Union(orderIDs);//两个集合的并集 并删除重复项
foreach (var item in customersWithOrders)
{
Console.Write("{0} ",item);
}*/
//-----------------------------------------Intersect、Excpt、Union用法 end----------------------------------------------------
//-------------------------------------------------join用法 begin----------------------------------------------------
var queryResults =
from c in customers
join o in orders on c.ID equals o.ID
select new { c.ID, c.City, SalesBefore = c.Sales, NewOrder = o.Amount, SalesAfter = c.Sales + o.Amount };
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
//-------------------------------------------------join用法 end----------------------------------------------------
Console.WriteLine();
}
}