public void LinqJoin06() { string[] names = { "C#", "VB", "F#" }; var lengths = from name in names select name.Length; List <Customer> customers = LinqHellper.GetCustomers(); List <Supplier> suppliers = LinqHellper.GetSupplier(); var supplierCusts = from sup in suppliers join cust in customers on new { sup.City, sup.Country } equals new { cust.City, cust.Country } into cs from c in cs.DefaultIfEmpty() //Remove DefaultIfEmpty method call to make this an inner join orderby sup.SupplierName select new { Country = sup.Country, City = sup.City, SupplierName = sup.SupplierName, CompanyName = c == null ? "(No customers)" : c.CompanyName }; foreach (var item in supplierCusts) { Debug.WriteLine("{0} ({1}, {2}): {3}", item.SupplierName, item.City, item.Country, item.CompanyName); } }
//This sample uses Count to return a list of customers and how many orders each has. public void Count03() { List <Customer> customers = LinqHellper.GetCustomers(); var orderCounts = from cust in customers select new { cust.CustomerID, OrderCount = cust.Orders.Count() }; foreach (var item in orderCounts) { Debug.WriteLine("Custormet Id:" + item.CustomerID + " Order Count:" + item.OrderCount); } }
public void LinqJoin01() { List <Customer> customers = LinqHellper.GetCustomers(); List <Supplier> suppliers = LinqHellper.GetSupplier(); var custSupJoin = from sup in suppliers join cust in customers on sup.Country equals cust.Country select new { Country = sup.Country, SupplierName = sup.SupplierName, CustomerName = cust.CompanyName }; foreach (var item in custSupJoin) { Debug.WriteLine("Country = {0}, Supplier = {1}, Customer = {2}", item.Country, item.SupplierName, item.CustomerName); } }
public void LinqTake02() { List <Customer> customers = LinqHellper.GetCustomers(); var first3WAOrders = ( from cust in customers from order in cust.Orders where cust.Region == "WA" select new { cust.CustomerID, order.OrderID, order.OrderDate }) .Take(3); Debug.WriteLine("First 3 orders in WA:"); foreach (var order in first3WAOrders) { Debug.WriteLine($"Customer ID: {order.CustomerID} Order ID: {order.OrderID} Order Date: {order.OrderDate}"); } }
public void LinqSkip02() { List <Customer> customers = LinqHellper.GetCustomers(); var waOrders = from cust in customers from order in cust.Orders where cust.Region == "WA" select new { cust.CustomerID, order.OrderID, order.OrderDate }; var allButFirst2Orders = waOrders.Skip(2); Debug.WriteLine("All but first 2 orders in WA:"); foreach (var order in allButFirst2Orders) { Debug.WriteLine($"Customer ID: {order.CustomerID} Order ID: {order.OrderID} Order Date: {order.OrderDate}"); } }
public void Restrict04() { List <Customer> customers = LinqHellper.GetCustomers(); var waCustomers = from cust in customers where cust.Region == "WA" select cust; Debug.WriteLine("Customers from Washington and their orders:"); foreach (var customer in waCustomers) { Debug.WriteLine("Customer {0}: {1}", customer.CustomerID, customer.CompanyName); foreach (var order in customer.Orders) { Debug.WriteLine(" Order {0}: {1}", order.OrderID, order.OrderDate); } } }
/// <summary> /// This sample uses Concat to create one sequence that contains the names of all customers and products, including any duplicates. /// </summary> public void LinqConcat02() { List <Customer> customers = LinqHellper.GetCustomers(); List <Product> products = LinqHellper.GetProducts(); var customerNames = from cust in customers select cust.CompanyName; var productNames = from prod in products select prod.ProductName; var allNames = customerNames.Concat(productNames); Debug.WriteLine("Customer and product names:"); foreach (var n in allNames) { Debug.WriteLine(n); } }
public void LinqJoin02() { List <Customer> customers = LinqHellper.GetCustomers(); List <Supplier> suppliers = LinqHellper.GetSupplier(); var custSupQuery = from sup in suppliers join cust in customers on sup.Country equals cust.Country into cs select new { Key = sup.Country, Items = cs }; foreach (var item in custSupQuery) { Debug.WriteLine(item.Key + ":"); foreach (var element in item.Items) { Debug.WriteLine(" " + element.CompanyName); } } }
public void LinqUnion02() { List <Product> products = LinqHellper.GetProducts(); List <Customer> customers = LinqHellper.GetCustomers(); var productFirstChars = from prod in products select prod.ProductName[0]; var customerFirstChars = from cust in customers select cust.CompanyName[0]; var uniqueFirstChars = productFirstChars.Union(customerFirstChars); Debug.WriteLine("Unique first letters from Product names and Customer names:"); foreach (var ch in uniqueFirstChars) { Debug.WriteLine(ch); } }
/// <summary> /// The Except operator produces the set difference between two sequences. /// The Except operator allocates and returns an enumerable object that captures the arguments passed to the operator. An ArgumentNullException is thrown if any argument is null. /// </summary> public void LinqExcept02() { List <Product> products = LinqHellper.GetProducts(); List <Customer> customers = LinqHellper.GetCustomers(); var productFirstChars = from prod in products select prod.ProductName[0]; var customerFirstChars = from cust in customers select cust.CompanyName[0]; var productOnlyFirstChars = productFirstChars.Except(customerFirstChars); Debug.WriteLine("First letters from Product names, but not from Customer names:"); foreach (var ch in productOnlyFirstChars) { Debug.WriteLine(ch); } }
public void LinqJoin05() { List <Customer> customers = LinqHellper.GetCustomers(); List <Supplier> suppliers = LinqHellper.GetSupplier(); var custSuppliers = from cust in customers join sup in suppliers on cust.Country equals sup.Country into ss from s in ss.DefaultIfEmpty() orderby cust.CompanyName select new { Country = cust.Country, CompanyName = cust.CompanyName, SupplierName = s == null ? "(No suppliers)" : s.SupplierName }; foreach (var item in custSuppliers) { Debug.WriteLine("{0} ({1}): {2}", item.CompanyName, item.Country, item.SupplierName); } }
public void LinqJoin04() { List <Customer> customers = LinqHellper.GetCustomers(); List <Supplier> suppliers = LinqHellper.GetSupplier(); var supplierCusts = from sup in suppliers join cust in customers on sup.Country equals cust.Country into cs from c in cs.DefaultIfEmpty() // DefaultIfEmpty preserves left-hand elements that have no matches on the right side orderby sup.SupplierName select new { Country = sup.Country, CompanyName = c == null ? "(No customers)" : c.CompanyName, SupplierName = sup.SupplierName }; foreach (var item in supplierCusts) { Debug.WriteLine("{0} ({1}): {2}", item.SupplierName, item.Country, item.CompanyName); } }