static void Main(string[] args) { LinqSamples samples = new LinqSamples(); //Comment or uncomment the method calls below to run or not samples.Linq102(); // This sample shows how to perform a simple inner equijoin of two sequences to produce // a flat result set that consists of each element in suppliers that has a matching // element in customers //samples.Linq103(); // A group join produces a hierarchical sequence. The following query is an inner join // that produces a sequence of objects, each of which has a key and an inner sequence of // all matching elements //samples.Linq104(); // The group join operator is more general than join, as this slightly more verbose // version of the cross join sample shows //samples.Linq105(); // For each customer in the table of customers, this query returns all the suppliers from // that same country, or else a string indicating that no suppliers from that country // were found //samples.Linq106(); // For each customer in the table of customers, this query returns all the suppliers from // that same country, or else a string indicating that no suppliers from that country // were found //samples.Linq107(); // For each supplier in the table of suppliers, this query returns all the customers from // the same city and country, or else a string indicating that no customers from that // city/country were found. Note the use of anonymous types to encapsulate the multiple // key values }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); //Comment or uncomment the method calls below to run or not samples.Linq20(); // This sample uses Take to get only the first 3 elements of the array //samples.Linq21(); // This sample uses Take to get the first 3 orders from customers in Washington //samples.Linq22(); // This sample uses Skip to get all but the first four elements of the array //samples.Linq23(); // This sample uses Take to get all but the first 2 orders from customers in Washington //samples.Linq24(); // This sample uses TakeWhile to return elements starting from the beginning of the array // until a number is read whose value is not less than 6 //samples.Linq25(); // This sample uses TakeWhile to return elements starting from the beginning of the array // until a number is hit that is less than its position in the array //samples.Linq26(); // This sample uses SkipWhile to get the elements of the array starting from the first // element divisible by 3 //samples.Linq27(); // This sample uses SkipWhile to get the elements of the array starting from the first // element less than its position }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); //Comment or uncomment the method calls below to run or not //samples.Linq1(); // This sample uses the where clause to find all elements of an array with a value // less than 5 //samples.Linq2(); // This sample uses the where clause to find all products that are out of stock Console.WriteLine("Linq3"); samples.Linq3(); // This sample uses the where clause to find all products that are in stock and cost // more than 3.00 per unit Console.WriteLine(); Console.WriteLine("Linq4"); samples.Linq4(); // This sample uses the where clause to find all customers in Washington and then it // uses a foreach loop to iterate over the orders collection that belongs to each // customer Console.WriteLine(); //samples.Linq5(); // This sample demonstrates an indexed where clause that returns digits whose name is // shorter than their value Console.ReadLine(); }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); //Comment or uncomment the method calls below to run or not samples.Linq46(); // This sample uses Distinct to remove duplicate elements in a sequence of factors of 300 //samples.Linq47(); // This sample uses Distinct to find the unique Category names //samples.Linq48(); // This sample uses Union to create one sequence that contains the unique values from both // arrays //samples.Linq49(); // This sample uses the Union method to create one sequence that contains the unique first // letter from both product and customer names. Union is only available through method // syntax //samples.Linq50(); // This sample uses Intersect to create one sequence that contains the common values shared // by both arrays //samples.Linq51(); // This sample uses Intersect to create one sequence that contains the common first letter // from both product and customer names //samples.Linq52(); // This sample uses Except to create a sequence that contains the values from numbersA that // are not also in numbersB //samples.Linq53(); // This sample uses Except to create one sequence that contains the 1st letters of product // names that are not also first letters of customer names }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); //Comment or uncomment the method calls below to run or not samples.Linq65(); // This sample uses Range to generate a sequence of numbers from 100 to 149 // that is used to find which numbers in that range are odd and even //samples.Linq66(); // This sample uses Repeat to generate a sequence that contains the number 7 // ten times }
public static void test() { LinqSamples samples = new LinqSamples(); // Comment or uncomment the method calls below to run or not //samples.XLinq6(); // Construct an XElement from string //samples.XLinq7(); // Add XML declaration to a document //samples.XLinq8(); // Computed element name //samples.XLinq9(); // Create a simple config file //samples.XLinq10(); // Create an XmlSchema //samples.XLinq11(); // Create an XML document with an XSLT PI //samples.XLinq12(); // XML comment construction //samples.XLinq13(); // Create a CData section samples.XLinq14(); // Create a sequence of customer elements }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); //Comment or uncomment the method calls below to run or not samples.Linq99(); // The following sample shows how query execution is deferred until the query is // enumerated at a foreach statement //samples.Linq100(); // The following sample shows how queries can be executed immediately, and their // results stored in memory, with methods such as ToList //samples.Linq101(); // The following sample shows how, because of deferred execution, queries can be //used again after data changes and will then operate on the new data }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); //Comment or uncomment the method calls below to run or not samples.Linq54(); // This sample uses ToArray to immediately evaluate a sequence into an array //samples.Linq55(); // This sample uses ToList to immediately evaluate a sequence into a List<T> //samples.Linq56(); // This sample uses ToDictionary to immediately evaluate a sequence and a // related key expression into a dictionary //samples.Linq57(); // This sample uses OfType to return only the elements of the array that are // of type double }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); //Comment or uncomment the method calls below to run or not samples.Linq67(); // This sample uses Any to determine if any of the words in the array contain the // substring 'ei' //samples.Linq69(); // This sample uses Any to return a grouped a list of products only for categories // that have at least one product that is out of stock //samples.Linq70(); // This sample uses All to determine whether an array contains only odd numbers //samples.Linq72(); // This sample uses All to return a grouped a list of products only for categories // that have all of their products in stock }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); //Comment or uncomment the method calls below to run or not samples.Linq94(); // This sample uses Concat to create one sequence that contains each array's // values, one after the other //samples.Linq95(); // This sample uses Concat to create one sequence that contains the names of all // customers and products, including any duplicates //samples.Linq96(); // This sample uses SequenceEquals to see if two sequences match on all elements // in the same order //samples.Linq97(); // This sample uses SequenceEqual to see if two sequences match on all elements // in the same order }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); //Comment or uncomment the method calls below to run or not // samples.Linq28(); // This sample uses orderby to sort a list of words alphabetically //samples.Linq29(); // This sample uses orderby to sort a list of words by length samples.Linq30(); // This sample uses orderby to sort a list of products by name. Use the \"descending\" // keyword at the end of the clause to perform a reverse ordering //samples.Linq31(); // This sample uses an OrderBy clause with a custom comparer to do a case-insensitive // sort of the words in an array //samples.Linq32(); // This sample uses orderby and descending to sort a list of doubles from highest to // lowest //samples.Linq33(); // This sample uses orderby to sort a list of products by units in stock from highest // to lowest //samples.Linq34(); // This sample uses method syntax to call OrderByDescending because it enables you to // use a custom comparer //samples.Linq35(); // This sample uses a compound orderby to sort a list of digits, first by length of // their name, and then alphabetically by the name itself //samples.Linq36(); // The first query in this sample uses method syntax to call OrderBy and ThenBy with a // custom comparer to sort first by word length and then by a case-insensitive sort of // the words in an array. The second two queries show another way to perform the same // task //samples.Linq37(); // This sample uses a compound orderby to sort a list of products, first by category, // and then by unit price, from highest to lowest //samples.Linq38(); // This sample uses an OrderBy and a ThenBy clause with a custom comparer to sort first // by word length and then by a case-insensitive descending sort of the words in an // array //samples.Linq39(); // This sample uses Reverse to create a list of all digits in the array whose second // letter is 'i' that is reversed from the order in the original array }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); //Comment or uncomment the method calls below to run or not samples.Linq67(); // This sample uses Any to determine if any of the words in the array contain the // substring 'ei' //samples.Linq69(); // This sample uses Any to return a grouped a list of products only for categories // that have at least one product that is out of stock //samples.Linq70(); // This sample uses All to determine whether an array contains only odd numbers //samples.Linq72(); // This sample uses All to return a grouped a list of products only for categories // that have all of their products in stock Console.ReadKey(); }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); //Comment or uncomment the method calls below to run or not //samples.Linq73(); // This sample uses Count to get the number of unique prime factors of 300 //samples.Linq74(); // This sample uses Count to get the number of odd ints in the array //samples.Linq76(); // This sample uses Count to return a list of customers and how many orders each has //samples.Linq77(); // This sample uses Count to return a list of categories and how many products each has //samples.Linq78(); // This sample uses Sum to add all the numbers in an array //samples.Linq79(); // This sample uses Sum to get the total number of characters of all words in the array samples.Linq80(); // This sample uses Sum to get the total units in stock for each product category //samples.Linq81(); // This sample uses Min to get the lowest number in an array //samples.Linq82(); // This sample uses Min to get the length of the shortest word in an array //samples.Linq83(); // This sample uses Min to get the cheapest price among each category's products //samples.Linq84(); // This sample uses Min to get the products with the lowest price in each category //samples.Linq85(); // This sample uses Max to get the highest number in an array. Note that the method // returns a single value //samples.Linq86(); // This sample uses Max to get the length of the longest word in an array //samples.Linq87(); // This sample uses Max to get the most expensive price among each category's products //samples.Linq88(); // This sample uses Max to get the products with the most expensive price in each category //samples.Linq89(); // This sample uses Average to get the average of all numbers in an array //samples.Linq90(); // This sample uses Average to get the average length of the words in the array //samples.Linq91(); // This sample uses Average to get the average price of each category's products //samples.Linq92(); // This sample uses Aggregate to create a running product on the array that calculates // the total product of all elements //samples.Linq93(); // This sample uses Aggregate to create a running account balance that subtracts each // withdrawal from the initial balance of 100, as long as the balance never drops below 0 Console.ReadLine(); }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); // Comment or uncomment the method calls below to run or not samples.DataSetLinq6(); // This sample uses select to produce a sequence of ints one higher than those in an existing array of ints. //samples.DataSetLinq7(); // This sample uses select to return a sequence of just the names of a list of products. //samples.DataSetLinq8(); // This sample uses select to produce a sequence of strings representing the text version of a sequence of ints. //samples.DataSetLinq9(); // This sample uses select to produce a sequence of the uppercase and lowercase versions of each word in the original array. //samples.DataSetLinq10(); // This sample uses select to produce a sequence containing text representations of digits and whether their length is even or odd. //samples.DataSetLinq11(); // This sample uses select to produce a sequence containing some properties of Products... //samples.DataSetLinq12(); // This sample uses an indexed Select clause to determine if the value of ints in an array match their position in the array. //samples.DataSetLinq13(); // This sample combines select and where to make a simple query that returns the text form of each digit less than 5. //samples.DataSetLinq14(); // This sample uses a compound from clause to make a query that returns all pairs of numbers... //samples.DataSetLinq15(); // This sample uses a compound from clause to select all orders where the order total is less than 500.00. //samples.DataSetLinq16(); // This sample uses a compound from clause to select all orders where the order was made in 1998 or later. //samples.DataSetLinq17(); // This sample uses a compound from clause to select all orders where order total is greater than 2000.00... //samples.DataSetLinq18(); // This sample uses multiple from clauses so that filtering on customers can be done before selecting their orders... //samples.DataSetLinq19(); // This sample uses an indexed SelectMany clause to select all orders... }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); //Comment or uncomment the method calls below to run or not samples.Linq94(); // This sample uses Concat to create one sequence that contains each array's // values, one after the other //samples.Linq95(); // This sample uses Concat to create one sequence that contains the names of all // customers and products, including any duplicates //samples.Linq96(); // This sample uses SequenceEquals to see if two sequences match on all elements // in the same order //samples.Linq97(); // This sample uses SequenceEqual to see if two sequences match on all elements // in the same order Console.ReadKey(); }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); //Comment or uncomment the method calls below to run or not samples.Linq58(); // This sample uses First to return the first matching element as a Product, instead // of as a sequence containing a Product //samples.Linq59(); // This sample uses First to find the first element in the array that starts with 'o' //samples.Linq61(); // This sample uses FirstOrDefault to try to return the first element of the sequence, // unless there are no elements, in which case the default value for that type is // returned. FirstOrDefault is useful for creating outer joins //samples.Linq62(); // This sample uses FirstOrDefault to return the first product whose ProductID is 789 // as a single Product object, unless there is no match, in which case null is returned //samples.Linq64(); // This sample uses ElementAt to retrieve the second number greater than 5 from an array }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); // Comment or uncomment the method calls below to run or not samples.XLinq18(); // Select all the customers in the xml document //samples.XLinq19(); // Select all the child elements of the first customer //samples.XLinq20(); // Select the first customer in the document //samples.XLinq21(); // Query for one child element on a sequence of elements //samples.XLinq22(); // Selects all the CustomerIDs in the xml document //samples.XLinq23(); // Find orders with price > 100 //samples.XLinq24(); // Get the root element of a document //samples.XLinq25(); // Filter query results using where //samples.XLinq26(); // Select all ContactName elements in the document //samples.XLinq27(); // Select all text in the document //samples.XLinq28(); // Check if two nodes belong to the same document //samples.XLinq29(); // Query for parent of an Element //samples.XLinq30(); // Add customer company info to orders of the first customer //samples.XLinq31(); // Query content of a given type of an existing element //samples.XLinq32(); // Query for all Swedish customer orders and Swedish orders whose freight is > 250 //samples.XLinq33(); // Query the 3rd customer in the document //samples.XLinq34(); // Union books authored by two authors: Serge and Peter //samples.XLinq35(); // Intersect books that are common for both authors //samples.XLinq36(); // Find books that are authored by Peter and did not have Serge as co-author //samples.XLinq37(); // Display the path to a node //samples.XLinq38(); // Check if 2 sequences of nodes are equal. Did Serge and Peter co-author all of their the books? //samples.XLinq39(); // List books until total price is less that $150 //samples.XLinq40(); // Create 5 new customers with different IDs //samples.XLinq41(); // Initialize new orders with items //samples.XLinq42(); // Check if there are any customers in Argentina //samples.XLinq43(); // Check if all books have at least one author //samples.XLinq44(); // Find the number of orders for a customer //samples.XLinq45(); // Find tax on an order //samples.XLinq46(); // Find all the countries where there is a customer //samples.XLinq47(); // List all books by Serge and Peter with co-authored books repeated //samples.XLinq48(); // Query the first two customers //samples.XLinq49(); // Skip the first 3 books //samples.XLinq50(); // Print items that dont fit in budget //samples.XLinq51(); // Get all books authored by Serge and Peter //samples.XLinq52(); // Find the container document of an element }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); //Comment or uncomment the method calls below to run or not //samples.Linq1(); // This sample uses the where clause to find all elements of an array with a value // less than 5 //samples.Linq2(); // This sample uses the where clause to find all products that are out of stock samples.Linq3(); // This sample uses the where clause to find all products that are in stock and cost // more than 3.00 per unit samples.Linq4(); // This sample uses the where clause to find all customers in Washington and then it // uses a foreach loop to iterate over the orders collection that belongs to each // customer //samples.Linq5(); // This sample demonstrates an indexed where clause that returns digits whose name is // shorter than their value }
public void Linq6_CustormersData_DeepEqualTrue() { // Arrange var linqSamples = new LinqSamples(new FakeDataSource(), Properties.Resources.FakeCustomersLinq6); IEnumerable <Customer> expected = new List <Customer>() { new Customer() { CustomerID = "ALFKI", CompanyName = "Alfreds Futterkiste", Address = "Obere Str. 57", City = "Berlin", Region = "BC", PostalCode = "12209", Country = "Germany", Phone = "030-0074321", Fax = "030-0076545", Orders = new Order[] { new Order() { OrderID = 10643, OrderDate = new DateTime(1997, 08, 25, 00, 00, 00), Total = 800 }, new Order() { OrderID = 10643, OrderDate = new DateTime(1997, 08, 25, 00, 00, 00), Total = 800 } } } }; // Act var actual = linqSamples.Linq6(); // Assert Assert.IsTrue(actual.IsDeepEqual(expected)); }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); //Comment or uncomment the method calls below to run or not samples.Linq73(); // This sample uses Count to get the number of unique prime factors of 300 //samples.Linq74(); // This sample uses Count to get the number of odd ints in the array //samples.Linq76(); // This sample uses Count to return a list of customers and how many orders each has //samples.Linq77(); // This sample uses Count to return a list of categories and how many products each has //samples.Linq78(); // This sample uses Sum to add all the numbers in an array //samples.Linq79(); // This sample uses Sum to get the total number of characters of all words in the array //samples.Linq80(); // This sample uses Sum to get the total units in stock for each product category //samples.Linq81(); // This sample uses Min to get the lowest number in an array //samples.Linq82(); // This sample uses Min to get the length of the shortest word in an array //samples.Linq83(); // This sample uses Min to get the cheapest price among each category's products //samples.Linq84(); // This sample uses Min to get the products with the lowest price in each category //samples.Linq85(); // This sample uses Max to get the highest number in an array. Note that the method // returns a single value //samples.Linq86(); // This sample uses Max to get the length of the longest word in an array //samples.Linq87(); // This sample uses Max to get the most expensive price among each category's products //samples.Linq88(); // This sample uses Max to get the products with the most expensive price in each category //samples.Linq89(); // This sample uses Average to get the average of all numbers in an array //samples.Linq90(); // This sample uses Average to get the average length of the words in the array //samples.Linq91(); // This sample uses Average to get the average price of each category's products //samples.Linq92(); // This sample uses Aggregate to create a running product on the array that calculates // the total product of all elements //samples.Linq93(); // This sample uses Aggregate to create a running account balance that subtracts each // withdrawal from the initial balance of 100, as long as the balance never drops below 0 }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); // Comment or uncomment the method calls below to run or not samples.XLinq60(); // Add an element as the last child //samples.XLinq61(); // Add an element as the first child //samples.XLinq62(); // Add multiple elements as children //samples.XLinq63(); // Add an attribute to an element //samples.XLinq64(); // Add attributes and elements //samples.XLinq65(); // Replace content to an existing element //samples.XLinq66(); // Remove content of an element //samples.XLinq67(); // Remove all content and attributes of an element //samples.XLinq68(); // Remove all attributes of an element //samples.XLinq69(); // Remove an attribute of an element //samples.XLinq70(); // Update the value of an attribute //samples.XLinq71(); // Remove a child element by name //samples.XLinq72(); // Update a child element by name //samples.XLinq73(); // Remove a list of elements //samples.XLinq74(); // Remove a list of attributes //samples.XLinq75(); // Add an un-parented element to an element //samples.XLinq76(); // Adding a parented element to another container clones it }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); //Comment or uncomment the method calls below to run or not samples.Linq46(); // This sample uses Distinct to remove duplicate elements in a sequence of factors of 300 Console.ReadKey(); samples.Linq47(); // This sample uses Distinct to find the unique Category names Console.ReadKey(); samples.Linq48(); // This sample uses Union to create one sequence that contains the unique values from both arrays Console.ReadKey(); samples.Linq49(); // This sample uses the Union method to create one sequence that contains the unique first letter from both product and customer names. Union is only available through method syntax Console.ReadKey(); samples.Linq50(); // This sample uses Intersect to create one sequence that contains the common values shared by both arrays Console.ReadKey(); samples.Linq51(); // This sample uses Intersect to create one sequence that contains the common first letter from both product and customer names Console.ReadKey(); samples.Linq52(); // This sample uses Except to create a sequence that contains the values from numbersA that are not also in numbersB Console.ReadKey(); samples.Linq53(); // This sample uses Except to create one sequence that contains the 1st letters of product names that are not also first letters of customer names Console.ReadKey(); }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); // Comment or uncomment the method calls below to run or not samples.DataSetLinq28(); // This sample uses orderby to sort a list of words alphabetically. //samples.DataSetLinq29(); // This sample uses orderby to sort a list of words by length. //samples.DataSetLinq30(); // This sample uses orderby to sort a list of products by name. //samples.DataSetLinq31(); // This sample uses an OrderBy clause with a custom comparer to do a case-insensitive sort of the words in an array. //samples.DataSetLinq32(); // This sample uses orderby and descending to sort a list of doubles from highest to lowest. //samples.DataSetLinq33(); // This sample uses orderby to sort a list of products by units in stock from highest to lowest. //samples.DataSetLinq34(); // This sample uses an OrderBy clause with a custom comparer to do a case-insensitive descending sort of the words in an array. //samples.DataSetLinq35(); // This sample uses a compound orderby to sort a list of digits, first by length of their name, and then alphabetically by the name itself. //samples.DataSetLinq36(); // This sample uses an OrderBy and a ThenBy clause with a custom comparer to sort first by word length and then by a case-insensitive sort of the words in an array. //samples.DataSetLinq37(); // This sample uses a compound orderby to sort a list of products, first by category, and then by unit price, from highest to lowest. //samples.DataSetLinq38(); // This sample uses an OrderBy and a ThenBy clause with a custom comparer to sort first by word length and then by a case-insensitive descending sort of the words in an array. //samples.DataSetLinq39(); // This sample uses Reverse to create a list of all digits in the array whose second letter is 'i' that is reversed from the order in the original array. //samples.DataSetLinq40(); // This sample uses group by to partition a list of numbers by their remainder when divided by 5. //samples.DataSetLinq41(); // This sample uses group by to partition a list of words by their first letter. //samples.DataSetLinq42(); // This sample uses group by to partition a list of products by category. //samples.DataSetLinq43(); // This sample uses group by to partition a list of each customer's orders, first by year, and then by month. //samples.DataSetLinq44(); // This sample uses GroupBy to partition trimmed elements of an array using a custom comparer that matches words that are anagrams of each other. //samples.DataSetLinq45(); // This sample uses GroupBy to partition trimmed elements of an array using a custom comparer that matches words that are anagrams of each other, and then converts the results to uppercase. }
public void Linq4_CustormersData_DeepEqualTrue() { // Arrange var linqSamples = new LinqSamples(new FakeDataSource(), Properties.Resources.FakeCustomersLinq4); var expected = new List <FirstOrder>() { new FirstOrder() { CustomerID = "ALFKI", Date = new DateTime(1997, 08, 25, 0, 0, 0) }, new FirstOrder() { CustomerID = "ANATR", Date = new DateTime(1996, 09, 18, 0, 0, 0) } }; // Act var actual = linqSamples.Linq4(); // Assert Assert.IsTrue(actual.IsDeepEqual(expected)); }
static void Main(string[] args) { // // Comment or uncomment the method calls below to run or not // var samples = new LinqSamples(); #region RestrictionOperators // This sample uses the where clause to find all elements of an array with a value // less than 5 samples.Linq1(); // This sample uses the where clause to find all products that are out of stock //samples.Linq2(); // This sample uses the where clause to find all products that are in stock and cost // more than 3.00 per unit //samples.Linq3(); // This sample uses the where clause to find all customers in Washington and then it // uses a foreach loop to iterate over the orders collection that belongs to each // customer //samples.Linq4(); // This sample demonstrates an indexed where clause that returns digits whose name is // shorter than their value //samples.Linq5(); #endregion #region ProjectionOperators // This sample uses select to produce a sequence of ints one higher than those in an existing array of ints. //samples.DataSetLinq6(); // This sample uses select to return a sequence of just the names of a list of products. //samples.DataSetLinq7(); // This sample uses select to produce a sequence of strings representing the text version of a sequence of ints. //samples.DataSetLinq8(); // This sample uses select to produce a sequence of the uppercase and lowercase versions of each word in the original array. //samples.DataSetLinq9(); // This sample uses select to produce a sequence containing text representations of digits and whether their length is even or odd. //samples.DataSetLinq10(); // This sample uses select to produce a sequence containing some properties of Products... //samples.DataSetLinq11(); // This sample uses an indexed Select clause to determine if the value of ints in an array match their position in the array. //samples.DataSetLinq12(); // This sample combines select and where to make a simple query that returns the text form of each digit less than 5. //samples.DataSetLinq13(); // This sample uses a compound from clause to make a query that returns all pairs of numbers... //samples.DataSetLinq14(); // This sample uses a compound from clause to select all orders where the order total is less than 500.00. //samples.DataSetLinq15(); // This sample uses a compound from clause to select all orders where the order was made in 1998 or later. //samples.DataSetLinq16(); // This sample uses a compound from clause to select all orders where order total is greater than 2000.00... //samples.DataSetLinq17(); // This sample uses multiple from clauses so that filtering on customers can be done before selecting their orders... //samples.DataSetLinq18(); // This sample uses an indexed SelectMany clause to select all orders... //samples.DataSetLinq19(); #endregion #region PartitioningOperators // This sample uses Take to get only the first 3 elements of the array //samples.Linq20(); // This sample uses Take to get the first 3 orders from customers in Washington //samples.Linq21(); // This sample uses Skip to get all but the first four elements of the array //samples.Linq22(); // This sample uses Take to get all but the first 2 orders from customers in Washington //samples.Linq23(); // This sample uses TakeWhile to return elements starting from the beginning of the array // until a number is read whose value is not less than 6 //samples.Linq24(); // This sample uses TakeWhile to return elements starting from the beginning of the array // until a number is hit that is less than its position in the array //samples.Linq25(); // This sample uses SkipWhile to get the elements of the array starting from the first // element divisible by 3 //samples.Linq26(); // This sample uses SkipWhile to get the elements of the array starting from the first // element less than its position //samples.Linq27(); #endregion #region OrderingOperators // This sample uses orderby to sort a list of words alphabetically //samples.Linq28(); // This sample uses orderby to sort a list of words by length //orderOps.Linq29(); // This sample uses orderby to sort a list of products by name. Use the \"descending\" // keyword at the end of the clause to perform a reverse ordering //orderOps.Linq30(); //orderOps.Linq31(); // This sample uses an OrderBy clause with a custom comparer to do a case-insensitive // sort of the words in an array //orderOps.Linq32(); // This sample uses orderby and descending to sort a list of doubles from highest to // lowest //orderOps.Linq33(); // This sample uses orderby to sort a list of products by units in stock from highest // to lowest //orderOps.Linq34(); // This sample uses method syntax to call OrderByDescending because it enables you to // use a custom comparer //orderOps.Linq35(); // This sample uses a compound orderby to sort a list of digits, first by length of // their name, and then alphabetically by the name itself //orderOps.Linq36(); // The first query in this sample uses method syntax to call OrderBy and ThenBy with a // custom comparer to sort first by word length and then by a case-insensitive sort of // the words in an array. The second two queries show another way to perform the same // task //orderOps.Linq37(); // This sample uses a compound orderby to sort a list of products, first by category, // and then by unit price, from highest to lowest //orderOps.Linq38(); // This sample uses an OrderBy and a ThenBy clause with a custom comparer to sort first // by word length and then by a case-insensitive descending sort of the words in an // array //orderOps.Linq39(); // This sample uses Reverse to create a list of all digits in the array whose second // letter is 'i' that is reversed from the order in the original array #endregion #region GroupingOperators // This sample uses group by to partition a list of numbers by their remainder when divided by 5. //samples.DataSetLinq40(); // This sample uses group by to partition a list of words by their first letter. //samples.DataSetLinq41(); // This sample uses group by to partition a list of products by category. //samples.DataSetLinq42(); // This sample uses group by to partition a list of each customer's orders, first by year, and then by month. //samples.DataSetLinq43(); // This sample uses GroupBy to partition trimmed elements of an array using a custom comparer // that matches words that are anagrams of each other. //samples.DataSetLinq44(); // This sample uses GroupBy to partition trimmed elements of an array using a custom comparer that matches words // that are anagrams of each other, and then converts the results to uppercase. //samples.DataSetLinq45(); #endregion #region SetOperators // This sample uses Distinct to remove duplicate elements in a sequence of factors of 300 //samples.Linq46(); // This sample uses Distinct to find the unique Category names //samples.Linq47(); // This sample uses Union to create one sequence that contains the unique values from both // arrays //samples.Linq48(); // This sample uses the Union method to create one sequence that contains the unique first // letter from both product and customer names. Union is only available through method // syntax //samples.Linq49(); // This sample uses Intersect to create one sequence that contains the common values shared // by both arrays //samples.Linq50(); // This sample uses Intersect to create one sequence that contains the common first letter // from both product and customer names //samples.Linq51(); // This sample uses Except to create a sequence that contains the values from numbersA that // are not also in numbersB //samples.Linq52(); // This sample uses Except to create one sequence that contains the 1st letters of product // names that are not also first letters of customer names //samples.Linq53(); #endregion #region ConversionOperators // This sample uses ToArray to immediately evaluate a sequence into an array //samples.Linq54(); // This sample uses ToList to immediately evaluate a sequence into a List<T> //samples.Linq55(); // This sample uses ToDictionary to immediately evaluate a sequence and a // related key expression into a dictionary //samples.Linq56(); // This sample uses OfType to return only the elements of the array that are // of type double //samples.Linq57(); #endregion #region ElementOperators // This sample uses First to return the first matching element as a Product, instead // of as a sequence containing a Product //samples.Linq58(); // This sample uses First to find the first element in the array that starts with 'o' //samples.Linq59(); // This sample uses FirstOrDefault to try to return the first element of the sequence, // unless there are no elements, in which case the default value for that type is // returned. FirstOrDefault is useful for creating outer joins //samples.Linq61(); // This sample uses FirstOrDefault to return the first product whose ProductID is 789 // as a single Product object, unless there is no match, in which case null is returned //samples.Linq62(); // This sample uses ElementAt to retrieve the second number greater than 5 from an array //samples.Linq64(); #endregion #region GenerationOperators // This sample uses Range to generate a sequence of numbers from 100 to 149 // that is used to find which numbers in that range are odd and even //samples.Linq65(); // This sample uses Repeat to generate a sequence that contains the number 7 // ten times //samples.Linq66(); #endregion #region Quantifiers // This sample uses Any to determine if any of the words in the array contain the // substring 'ei' //samples.Linq67(); // This sample uses Any to return a grouped a list of products only for categories // that have at least one product that is out of stock //samples.Linq69(); // This sample uses All to determine whether an array contains only odd numbers //samples.Linq70(); // This sample uses All to return a grouped a list of products only for categories // that have all of their products in stock //samples.Linq72(); #endregion #region AggregateOperators // This sample uses Count to get the number of unique prime factors of 300 //samples.Linq73(); // This sample uses Count to get the number of odd ints in the array //samples.Linq74(); // This sample uses Count to return a list of customers and how many orders each has //samples.Linq76(); // This sample uses Count to return a list of categories and how many products each has //samples.Linq77(); // This sample uses Sum to add all the numbers in an array //samples.Linq78(); // This sample uses Sum to get the total number of characters of all words in the array //samples.Linq79(); // This sample uses Sum to get the total units in stock for each product category //samples.Linq80(); // This sample uses Min to get the lowest number in an array //samples.Linq81(); // This sample uses Min to get the length of the shortest word in an array //samples.Linq82(); // This sample uses Min to get the cheapest price among each category's products //samples.Linq83(); // This sample uses Min to get the products with the lowest price in each category //samples.Linq84(); // This sample uses Max to get the highest number in an array. Note that the method // returns a single value //samples.Linq85(); // This sample uses Max to get the length of the longest word in an array //samples.Linq86(); // This sample uses Max to get the most expensive price among each category's products //samples.Linq87(); // This sample uses Max to get the products with the most expensive price in each category //samples.Linq88(); // This sample uses Average to get the average of all numbers in an array //samples.Linq89(); // This sample uses Average to get the average length of the words in the array //samples.Linq90(); // This sample uses Average to get the average price of each category's products //samples.Linq91(); // This sample uses Aggregate to create a running product on the array that calculates // the total product of all elements //samples.Linq92(); // This sample uses Aggregate to create a running account balance that subtracts each // withdrawal from the initial balance of 100, as long as the balance never drops below 0 //samples.Linq93(); #endregion #region MiscellaneousOperators // This sample uses Concat to create one sequence that contains each array's // values, one after the other //samples.Linq94(); // This sample uses Concat to create one sequence that contains the names of all // customers and products, including any duplicates //samples.Linq95(); // This sample uses SequenceEquals to see if two sequences match on all elements // in the same order //samples.Linq96(); // This sample uses SequenceEqual to see if two sequences match on all elements // in the same order //samples.Linq97(); #endregion #region CustomSequenceOperators // This sample uses a user-created sequence operator, Combine, to calculate the dot product of two vectors. //samples.DataSetLinq98(); #endregion #region QueryExecution // The following sample shows how query execution is deferred until the query is // enumerated at a foreach statement //samples.Linq99(); // The following sample shows how queries can be executed immediately, and their // results stored in memory, with methods such as ToList //samples.Linq100(); // The following sample shows how, because of deferred execution, queries can be // used again after data changes and will then operate on the new data //samples.Linq101(); #endregion #region JoinOperators // This sample shows how to perform a simple inner equijoin of two sequences to produce // a flat result set that consists of each element in suppliers that has a matching // element in customers //samples.Linq102(); // A group join produces a hierarchical sequence. The following query is an inner join // that produces a sequence of objects, each of which has a key and an inner sequence of // all matching elements //samples.Linq103(); // The group join operator is more general than join, as this slightly more verbose // version of the cross join sample shows //samples.Linq104(); // For each customer in the table of customers, this query returns all the suppliers from // that same country, or else a string indicating that no suppliers from that country // were found //samples.Linq105(); // For each customer in the table of customers, this query returns all the suppliers from // that same country, or else a string indicating that no suppliers from that country // were found //samples.Linq106(); // For each supplier in the table of suppliers, this query returns all the customers from // the same city and country, or else a string indicating that no customers from that // city/country were found. Note the use of anonymous types to encapsulate the multiple // key values //samples.Linq107(); #endregion }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); #region Restriction Operators 1 to 5 samples.Linq1(); // This sample uses the where clause to find all elements of an array with a value less than 5 samples.Linq2(); // This sample uses the where clause to find all products that are out of stock samples.Linq3(); // This sample uses the where clause to find all products that are in stock and cost more than 3.00 per unit samples.Linq4(); // This sample uses the where clause to find all customers in Washington and then it uses a foreach loop to iterate over the orders collection that belongs to each customer samples.Linq5(); // This sample demonstrates an indexed where clause that returns digits whose name is shorter than their value #endregion Restriction Operators 1 to 5 #region Projection Operators 6 to 19 samples.DataSetLinq6(); // This sample uses select to produce a sequence of ints one higher than those in an existing array of ints. samples.DataSetLinq7(); // This sample uses select to return a sequence of just the names of a list of products. samples.DataSetLinq8(); // This sample uses select to produce a sequence of strings representing the text version of a sequence of ints. samples.DataSetLinq9(); // This sample uses select to produce a sequence of the uppercase and lowercase versions of each word in the original array. samples.DataSetLinq10(); // This sample uses select to produce a sequence containing text representations of digits and whether their length is even or odd. samples.DataSetLinq11(); // This sample uses select to produce a sequence containing some properties of Products... samples.DataSetLinq12(); // This sample uses an indexed Select clause to determine if the value of ints in an array match their position in the array. samples.DataSetLinq13(); // This sample combines select and where to make a simple query that returns the text form of each digit less than 5. samples.DataSetLinq14(); // This sample uses a compound from clause to make a query that returns all pairs of numbers... samples.DataSetLinq15(); // This sample uses a compound from clause to select all orders where the order total is less than 500.00. samples.DataSetLinq16(); // This sample uses a compound from clause to select all orders where the order was made in 1998 or later. samples.DataSetLinq17(); // This sample uses a compound from clause to select all orders where order total is greater than 2000.00... samples.DataSetLinq18(); // This sample uses multiple from clauses so that filtering on customers can be done before selecting their orders... samples.DataSetLinq19(); // This sample uses an indexed SelectMany clause to select all orders... #endregion Projection Operators 6 to 19 #region Partitioning Operators 20 to 27 samples.Linq20(); // This sample uses Take to get only the first 3 elements of the array samples.Linq21(); // This sample uses Take to get the first 3 orders from customers in Washington samples.Linq22(); // This sample uses Skip to get all but the first four elements of the array samples.Linq23(); // This sample uses Take to get all but the first 2 orders from customers in Washington samples.Linq24(); // This sample uses TakeWhile to return elements starting from the beginning of the array until a number is read whose value is not less than 6 samples.Linq25(); // This sample uses TakeWhile to return elements starting from the beginning of the array until a number is hit that is less than its position in the array samples.Linq26(); // This sample uses SkipWhile to get the elements of the array starting from the first element divisible by 3 samples.Linq27(); // This sample uses SkipWhile to get the elements of the array starting from the first element less than its position #endregion Partitioning Operators 20 to 27 #region Ordering Operators 28 to 39 samples.Linq28(); // This sample uses orderby to sort a list of words alphabetically samples.Linq29(); // This sample uses orderby to sort a list of words by length samples.Linq30(); // This sample uses orderby to sort a list of products by name. Use the \"descending\" keyword at the end of the clause to perform a reverse ordering samples.Linq31(); // This sample uses an OrderBy clause with a custom comparer to do a case-insensitive sort of the words in an array samples.Linq32(); // This sample uses orderby and descending to sort a list of doubles from highest to lowest samples.Linq33(); // This sample uses orderby to sort a list of products by units in stock from highest to lowest samples.Linq34(); // This sample uses method syntax to call OrderByDescending because it enables you to use a custom comparer samples.Linq35(); // This sample uses a compound orderby to sort a list of digits, first by length of their name, and then alphabetically by the name itself samples.Linq36(); // The first query in this sample uses method syntax to call OrderBy and ThenBy with a // custom comparer to sort first by word length and then by a case-insensitive sort of // the words in an array. The second two queries show another way to perform the same task samples.Linq37(); // This sample uses a compound orderby to sort a list of products, first by category, and then by unit price, from highest to lowest samples.Linq38(); // This sample uses an OrderBy and a ThenBy clause with a custom comparer to sort first // by word length and then by a case-insensitive descending sort of the words in an array samples.Linq39(); // This sample uses Reverse to create a list of all digits in the array whose second letter is 'i' that is reversed from the order in the original array #endregion OrderingOperators # region Grouping Operators 40 to 45
private static void Main(string[] args) { LinqSamples samples = new LinqSamples(); #region Restriction Operators // samples.Linq1(); // samples.Linq2(); // samples.Linq3(); // samples.Linq4(); // samples.Linq5(); #endregion Restriction Operators #region Projection Operators // samples.Linq6(); // samples.Linq7(); // samples.Linq8(); // samples.Linq9(); // samples.Linq10(); // samples.Linq11(); // samples.Linq12(); // samples.Linq13(); // samples.Linq14(); // samples.Linq15(); // samples.Linq16(); // samples.Linq17(); // samples.Linq18(); // samples.Linq19(); #endregion Projection Operators #region Partitioning Operators // samples.Linq20(); // samples.Linq20_extend_by_me(); // samples.Linq21(); // samples.Linq22(); // samples.Linq23(); // samples.Linq24(); // samples.Linq25(); // samples.Linq26(); // samples.Linq27(); #endregion Partitioning Operators #region Ordering Operators // samples.Linq28(); // samples.Linq29(); // samples.Linq30(); // samples.Linq31(); // samples.Linq32(); // samples.Linq33(); // samples.Linq34(); // samples.Linq35(); // samples.Linq36(); // samples.Linq37(); // samples.Linq38(); // samples.Linq39(); #endregion Ordering Operators #region Grouping Operators // samples.Linq40(); // samples.Linq41(); // samples.Linq42(); // samples.Linq43(); // samples.Linq44(); // samples.Linq45(); #endregion Grouping Operators #region Grouping Operators // samples.Linq46(); // samples.Linq47(); // samples.Linq48(); // samples.Linq49(); // samples.Linq50(); // samples.Linq51(); // samples.Linq52(); // samples.Linq53(); #endregion Grouping Operators #region Conversion Operators // samples.Linq54(); // samples.Linq55(); // samples.Linq56(); // samples.Linq57(); #endregion Conversion Operators #region Element Operators // samples.Linq58(); // samples.Linq59(); // samples.Linq61(); // samples.Linq62(); // samples.Linq64(); #endregion Element Operators #region Generation Operators // samples.Linq65(); // samples.Linq66(); #endregion Generation Operators #region Quantifiers // samples.Linq67(); // samples.Linq69(); // samples.Linq70(); // samples.Linq72(); #endregion Quantifiers #region Aggregate Operators // samples.Linq73(); // samples.Linq74(); // samples.Linq76(); // samples.Linq77(); // samples.Linq78(); // samples.Linq79(); // samples.Linq80(); // samples.Linq81(); // samples.Linq82(); // samples.Linq83(); // samples.Linq84(); // samples.Linq85(); // samples.Linq86(); // samples.Linq87(); // samples.Linq88(); // samples.Linq89(); // samples.Linq90(); // samples.Linq91(); // samples.Linq92(); // samples.Linq93(); #endregion Aggregate Operators #region Miscellaneous Operators // samples.Linq94(); // samples.Linq95(); // samples.Linq96(); // samples.Linq97(); #endregion Miscellaneous Operators #region Custom Sequence Operators samples.Linq98(); #endregion Custom Sequence Operators #region Query Execution // samples.Linq99(); // samples.Linq100(); // samples.Linq101(); #endregion Query Execution #region Join Operators // samples.Linq102(); // samples.Linq103(); // samples.Linq104(); // samples.Linq105(); #endregion Join Operators Console.ReadKey(); }
static void Main(string[] args) { LinqSamples samples = new LinqSamples(); samples.Linq53(); }
static void Main(string[] args) { //Need Help? //see https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b LinqSamples samples = new LinqSamples(); List <Customer> customers = samples.GetCustomerList(); List <Product> productList = samples.GetProductList(); //1. Return a list of customers, ordered by customer name ascending List <Customer> cst = (from c in customers orderby c.CompanyName select c).ToList(); samples.WriteCustomers(cst); //2. Return a list of products, ordered by unit price ascending List <Product> prd = (from p in productList orderby p.UnitPrice select p).ToList(); samples.WriteProducts(prd); //3. Return the number of products and number of customers int count = (from p in productList select p).Count() + (from c in customers select c).Count(); // this is the way to do what I think you want in LINQ // you could also do int count = productList.Count() + customers.Count(); Console.WriteLine(count); //4. Return whether there are any products categorized as "Seafood" bool seaFood = productList.Any(prd2 => prd2.Category == "Seafood"); Console.WriteLine(seaFood); //5. Return whether all products are less than $100 per unit bool lessThan = !productList.Any(prd2 => prd2.UnitPrice > 100); Console.WriteLine(lessThan); //6. Return the average price per unit of all products in the catalog decimal avgPrice = (from p in productList select p.UnitPrice).ToList().Average(); Console.WriteLine(avgPrice); //7. Return all customers who are from the country "UK" List <Customer> cstUK = (from c in customers where c.Country == "UK" select c).ToList(); samples.WriteCustomers(cstUK); //8. Return the list of distinct product categories List <string> prdDistinct = (from p in productList select p.Category).Distinct().ToList(); foreach (string cat in prdDistinct) { Console.WriteLine(cat); } //9. Return the product (by name) with the lowest and the highest prices per unit decimal max = productList.Max(prd3 => prd3.UnitPrice); decimal min = productList.Min(prd3 => prd3.UnitPrice); string minName = productList.First(prd3 => prd3.UnitPrice == max).ProductName; string maxName = productList.First(prd3 => prd3.UnitPrice == min).ProductName; Console.WriteLine(minName); Console.WriteLine(maxName); //10. Return the cost to buy one unit of each product in the catalog List <decimal> prdPrices = (from p in productList select p.UnitPrice).ToList(); foreach (decimal price in prdPrices) { Console.WriteLine(price); } Console.ReadLine(); }
static void Main(string[] args) { var d = new LinqSamples(); Console.WriteLine("Task 1:"); foreach (var emp in LinqSamples.Task1()) { Console.WriteLine(emp); } Console.WriteLine("\nTask 2:"); foreach (var emp in LinqSamples.Task2()) { Console.WriteLine(emp); } Console.WriteLine("\nTask 3:"); Console.WriteLine(LinqSamples.Task3()); Console.WriteLine("\nTask 4:"); foreach (var emp in LinqSamples.Task4()) { Console.WriteLine(emp); } Console.WriteLine("\nTask 5:"); foreach (var emp in LinqSamples.Task5()) { Console.WriteLine(emp); } Console.WriteLine("\nTask 6:"); foreach (var emp in LinqSamples.Task6()) { Console.WriteLine(emp); } Console.WriteLine("\nTask 7:"); foreach (var job in LinqSamples.Task7()) { Console.WriteLine(job); } Console.WriteLine("\nTask 8:"); Console.Write("Is there \"Backend programmer\" in the collection? "); Console.WriteLine(LinqSamples.Task8() ? "Yes" : "No"); Console.WriteLine("\nTask 9:"); Console.WriteLine(LinqSamples.Task9()); Console.WriteLine("\nTask 10:"); foreach (var emp in LinqSamples.Task10()) { Console.WriteLine(emp); } Console.WriteLine("\nTask 11:"); Console.WriteLine(LinqSamples.Task11()); Console.WriteLine("\nTask 12:"); foreach (var emp in LinqSamples.Task12()) { Console.WriteLine(emp); } }
private static void Main(string[] args) { var d = new LinqSamples(); d.Task10(); }
public void Linq3_MoCK_Given10000_DeepEqualTrue() { // Arrange Mock <IDataSource> mockData = new Mock <IDataSource>(); mockData.SetupProperty(x => x.Customers, new List <Customer>() { new Customer() { CustomerID = "ALFKI", CompanyName = "Alfreds Futterkiste", Address = "Obere Str. 57", City = "Berlin", Region = null, PostalCode = "12209", Country = "Germany", Phone = "030-0074321", Fax = "030-0076545", Orders = new Order[] { new Order() { OrderID = 10643, OrderDate = new DateTime(1997, 08, 25, 00, 00, 00), Total = 8800 }, new Order() { OrderID = 10643, OrderDate = new DateTime(1997, 08, 25, 00, 00, 00), Total = 800 } } }, new Customer() { CustomerID = "hhhhh", CompanyName = "Alfreds Futterkiste", Address = "Obere Str. 57", City = "Berlin", Region = null, PostalCode = "12209", Country = "Germany", Phone = "030-0074321", Fax = "030-0076545", Orders = new Order[] { new Order() { OrderID = 10643, OrderDate = new DateTime(1997, 08, 25, 00, 00, 00), Total = 100 }, new Order() { OrderID = 10643, OrderDate = new DateTime(1997, 08, 25, 00, 00, 00), Total = 200 } } } }); var linqSamples = new LinqSamples(mockData.Object, null); int count = 8700; IEnumerable <Customer> expected = new List <Customer>() { new Customer() { CustomerID = "ALFKI", CompanyName = "Alfreds Futterkiste", Address = "Obere Str. 57", City = "Berlin", Region = null, PostalCode = "12209", Country = "Germany", Phone = "030-0074321", Fax = "030-0076545", Orders = new Order[] { new Order() { OrderID = 10643, OrderDate = new DateTime(1997, 08, 25, 00, 00, 00), Total = 8800 }, new Order() { OrderID = 10643, OrderDate = new DateTime(1997, 08, 25, 00, 00, 00), Total = 800 } } } }; // Act var actual = linqSamples.Linq3(count); // Assert Assert.IsTrue(actual.IsDeepEqual(expected)); }