static void Main(string[] args) { NorthwindDataContext nw = new NorthwindDataContext(); //The following queries examples are from Microsoft. //The examples from MS did not include the foreach loop or the WriteLine. I added those. //See Sample.cs for samples I did while in class. // // Query #1. List <int> numbers = new List <int>() { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; // The query variable can also be implicitly typed by using var Console.WriteLine("Filtering Query"); IEnumerable <int> filteringQuery = from num in numbers where num < 3 || num > 7 select num; foreach (var num in filteringQuery) { Console.WriteLine(num); } Console.WriteLine(); Console.WriteLine(); // Query #2. Console.WriteLine("Ordering Query"); IEnumerable <int> orderingQuery = from num in numbers where num < 3 || num > 7 orderby num ascending select num; foreach (var num in orderingQuery) { Console.WriteLine(num); } Console.WriteLine(); Console.WriteLine(); // Query #3. Console.WriteLine("Groupping Query"); string[] groupingQuery = { "carrots", "cabbage", "broccoli", "beans", "barley" }; IEnumerable <IGrouping <char, string> > queryFoodGroups = from item in groupingQuery group item by item[0]; foreach (var item in groupingQuery) { Console.WriteLine(item); } Console.WriteLine(); Console.WriteLine(); List <int> numbers1 = new List <int>() { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; List <int> numbers2 = new List <int>() { 15, 14, 11, 13, 19, 18, 16, 17, 12, 10 }; Console.WriteLine("Average"); // Query #4. var average = numbers1.Average(); Console.WriteLine(average); Console.WriteLine(); Console.WriteLine(); // Query #5. Console.WriteLine("Concatenation Query"); var concatenationQuery = numbers1.Concat(numbers2); foreach (var item in concatenationQuery) { Console.WriteLine(item); } Console.WriteLine(); Console.WriteLine(); // Query #6. Console.WriteLine("Large Number Query"); var largeNumbersQuery = numbers2.Where(c => c > 15); foreach (var item in largeNumbersQuery) { Console.WriteLine(item); } Console.WriteLine(); Console.WriteLine(); // Query #7. Console.WriteLine("Numbers Query"); // Using a query expression with method syntax int numCount1 = (from num in numbers1 where num < 3 || num > 7 select num).Count(); // Better: Create a new variable to store // the method call result IEnumerable <int> numbersQuery = from num in numbers1 where num < 3 || num > 7 select num; int numCount2 = numbersQuery.Count(); foreach (var item in numbersQuery) { Console.WriteLine(item); } Console.WriteLine(); Console.WriteLine("Numbers Query Using var"); //The previous query can be written by using implicit typing with var, as follows: var numCount = from num in numbers1 where num < 3 || num > 7 select num; int numCount3 = numCount.Count(); foreach (var item in numCount) { Console.WriteLine(item); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Group Join Query"); //This query creates a group join, and then sorts the groups based on the category element, //which is still in scope. Inside the anonymous type initializer, a sub-query orders all the matching //elements from the products sequence. var groupJoinQuery2 = from category in nw.Categories join prod in nw.Products on category.CategoryID equals prod.CategoryID into prodGroup orderby category.CategoryName select new { Category = category.CategoryName, Products = from prod2 in prodGroup orderby prod2.ProductName select prod2 }; foreach (var productGroup in groupJoinQuery2) { Console.WriteLine(productGroup.Category); foreach (var prodItem in productGroup.Products) { Console.WriteLine(" {0,-10} {1}", prodItem.ProductName, prodItem.CategoryID); } } Console.ReadLine(); }
static void Samples(string[] args) { NorthwindDataContext nw = new NorthwindDataContext(); //Int array int[] numbers = { 5, 4, 1, 3, 9, 8, 7, 2, 0 }; //String array string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var shortDigits = digits.Where((digit, index) => digit.Length < index); Console.WriteLine("Short Digits"); foreach (var d in shortDigits) { Console.WriteLine("The word {0} is shorter than it's value", d); } Console.WriteLine(); Console.WriteLine(); var textNums = from n in numbers select digits[n]; foreach (var t in textNums) { Console.WriteLine(t); } Console.WriteLine(); Console.WriteLine(); var products = from p in nw.Products select new { p.ProductName, p.CategoryID, p.UnitPrice }; foreach (var w in products) { Console.WriteLine("{0} is in the category {1} and costs {2}", w.ProductName, w.CategoryID, w.UnitPrice); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Query with the where clause."); //Querys with the where clause. var orders = from c in nw.Customers from o in c.Orders where o.OrderDate >= new DateTime(1998, 1, 1) select new { c.CustomerID, o.OrderID, o.OrderDate }; foreach (var order in orders) { Console.WriteLine("Order with an id of {0}, is for the customer with an id of {1} was placed on {2}", order.OrderID, order.CustomerID, order.OrderDate); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Query with the where clause."); Product prod = (from p in nw.Products where p.ProductID == 12 select p).FirstOrDefault(); Console.WriteLine(prod.ProductName); Console.WriteLine(); Console.WriteLine(); string[] words = { "belief", "relief", "reciept", "field" }; var iAfterE = words.Any(w => w.Contains("ie")); Console.WriteLine("The list coantains a word or words that contain 'ie' is {0}", iAfterE); Console.WriteLine(); Console.WriteLine(); ////This is broken: //var q = from c in nw.Categories // join p in nw.Products on c.CategoryID equals p.CategoryID into ps // from x in ps.DefaultIfEmpty() // select new // { // CategID = c.CategoryID, ProductName = p.CategoryID == null // } var cats = from c in nw.Categories select c; foreach (var cat in cats) { Console.WriteLine("The category description is {0}", cat.Description); } Console.ReadLine(); }