private static void SelectManyCompoundFrom2() { Console.WriteLine("This sample uses a compound from clause to select all orders where the order total is less than 500.00."); List <Customer> customers = factory.GetCustomerList(); var orders = from customer in customers from order in customer.Orders where order.Total < 500.00M select new { customer.CustomerID, order.OrderID, order.Total }; foreach (var order in orders) { Console.WriteLine("Customer ID = {0}, Order Id = {1}, Total = {2}", order.CustomerID, order.OrderID, order.Total); } }
private static void TakeNested() { Console.WriteLine("This sample uses Take to get the first 3 orders from customers in Washington."); List <Customer> customers = factory.GetCustomerList(); var first3WAOrders = from customer in customers where customer.Region == "WA" from order in customer.Orders.Take(3) select new { customer.CustomerID, order.OrderID, order.OrderDate }; Console.WriteLine("First 3 orders in WA:"); foreach (var order in first3WAOrders) { Console.WriteLine("Customer ID = {0}, Order Id = {1}, Total = {2}", order.CustomerID, order.OrderID, order.OrderDate); } }
private static void UnionII() { Console.WriteLine("This sample uses Union to create one sequence that contains the unique first letter from both product and customer names."); List <Product> products = factory.GetProductList(); List <Customer> customers = factory.GetCustomerList(); var productFirstChars = from product in products select product.ProductName[0]; var customerFirstChars = from customer in customers select customer.CompanyName[0]; var uniqueFirstChars = productFirstChars.Union(customerFirstChars); Console.WriteLine("Unique first letters from Product names and Customer names:"); foreach (var ch in uniqueFirstChars) { Console.WriteLine(ch); } }
private static void GroupByNested() { Console.WriteLine("This sample uses group by to partition a list of each customer's orders, first by year, and then by month."); List <Customer> customers = factory.GetCustomerList(); var customerOrderGroups = from customer in customers select new { customer.CompanyName, YearGroups = from order in customer.Orders group order by order.OrderDate.Year into yearGroup select new { Year = yearGroup.Key, MonthGroups = from order in yearGroup group order by order.OrderDate.Month into monthGroup select new { Month = monthGroup.Key, Orders = monthGroup } } }; foreach (var item in customerOrderGroups) { Console.WriteLine("Company Name = {0}, Year Groups ", item.CompanyName); foreach (var yearGroup in item.YearGroups) { Console.WriteLine("Year : {0}", yearGroup.Year); foreach (var monthGroup in yearGroup.MonthGroups) { Console.WriteLine("Month : {0}", monthGroup.Month); foreach (var monthOrders in monthGroup.Orders) { Console.WriteLine("Order ID : = {0}, Total = {1} ", monthOrders.OrderID, monthOrders.Total); } } } } }
private static void CountNested() { Console.WriteLine("This sample uses Count to return a list of customers and how many orders each has."); List <Customer> customers = factory.GetCustomerList(); var orderCounts = from customer in customers select new { Customer = customer.CustomerID, OrderCount = customer.Orders.Count() }; foreach (var item in orderCounts) { Console.WriteLine("Customer ID = {0}, Order Count = {1}", item.Customer, item.OrderCount); } }
public static void WhereDrillDown() { Console.WriteLine("This sample uses where to find all customers in Washington and then uses the resulting sequence to drill down into their orders."); List <Customer> customers = factory.GetCustomerList(); var waCustomers = from customer in customers where customer.Region == "WA" select customer; Console.WriteLine("Customers from Washington and their orders:"); foreach (Customer customer in waCustomers) { Console.WriteLine("Customer {0}: {1}", customer.CustomerID, customer.CompanyName); foreach (Order order in customer.Orders) { Console.WriteLine(" Order {0}: {1}", order.OrderID, order.OrderDate); } } }
private static void ConcatII() { Console.WriteLine("This sample uses Concat to create one sequence that contains the names of all customers and products, including any duplicates."); List <Customer> customers = factory.GetCustomerList(); List <Product> products = factory.GetProductList(); var customerNames = from customer in customers select customer.CompanyName; var productNames = from product in products select product.ProductName; var allNames = customerNames.Concat(productNames); Console.WriteLine("Customer and product names:"); foreach (var n in allNames) { Console.WriteLine(n); } }