Sunday 29 June 2014

LINQ - Set Operators

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ComponentModel; 
using System.Xml.Linq; 
 
namespace SetOperators 
{ 
    class Program 
    { 
        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 
        } 
 
        public class Product 
        { 
            public int ProductID { getset; } 
            public string ProductName { getset; } 
            public string Category { getset; } 
            public decimal UnitPrice { getset; } 
            public int UnitsInStock { getset; } 
        } 
 
        public class Order 
        { 
            public int OrderID { getset; } 
            public DateTime OrderDate { getset; } 
            public decimal Total { getset; } 
        } 
 
        public class Customer 
        { 
            public string CustomerID { getset; } 
            public string CompanyName { getset; } 
            public string Address { getset; } 
            public string City { getset; } 
            public string Region { getset; } 
            public string PostalCode { getset; } 
            public string Country { getset; } 
            public string Phone { getset; } 
            public string Fax { getset; } 
            public Order[] Orders { getset; } 
        } 
 
        class LinqSamples 
        { 
            private List<Product> productList; 
            private List<Customer> customerList; 
 
            [Category("Set Operators")] 
            [Description("This sample uses Distinct to remove duplicate elements in a sequence of " + 
                        "factors of 300.")] 
            public void Linq46() 
            { 
                int[] factorsOf300 = { 22355 }; 
 
                var uniqueFactors = factorsOf300.Distinct(); 
 
                Console.WriteLine("Prime factors of 300:"); 
                foreach (var f in uniqueFactors) 
                { 
                    Console.WriteLine(f); 
                } 
            } 
 
            [Category("Set Operators")] 
            [Description("This sample uses Distinct to find the unique Category names.")] 
            public void Linq47() 
            { 
                List<Product> products = GetProductList(); 
 
                var categoryNames = ( 
                    from prod in products 
                    select prod.Category) 
                    .Distinct(); 
 
                Console.WriteLine("Category names:"); 
                foreach (var n in categoryNames) 
                { 
                    Console.WriteLine(n); 
                } 
            } 
 
            [Category("Set Operators")] 
            [Description("This sample uses Union to create one sequence that contains the unique values " + 
                         "from both arrays.")] 
            public void Linq48() 
            { 
                int[] numbersA = { 0245689 }; 
                int[] numbersB = { 13578 }; 
 
                var uniqueNumbers = numbersA.Union(numbersB); 
 
                Console.WriteLine("Unique numbers from both arrays:"); 
                foreach (var n in uniqueNumbers) 
                { 
                    Console.WriteLine(n); 
                } 
            } 
 
            [Category("Set Operators")] 
            [Description("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.")] 
            public void Linq49() 
            { 
                List<Product> products = GetProductList(); 
                List<Customer> customers = GetCustomerList(); 
 
                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); 
 
                Console.WriteLine("Unique first letters from Product names and Customer names:"); 
                foreach (var ch in uniqueFirstChars) 
                { 
                    Console.WriteLine(ch); 
                } 
            } 
 
            [Category("Set Operators")] 
            [Description("This sample uses Intersect to create one sequence that contains the common values " + 
                        "shared by both arrays.")] 
            public void Linq50() 
            { 
                int[] numbersA = { 0245689 }; 
                int[] numbersB = { 13578 }; 
 
                var commonNumbers = numbersA.Intersect(numbersB); 
 
                Console.WriteLine("Common numbers shared by both arrays:"); 
                foreach (var n in commonNumbers) 
                { 
                    Console.WriteLine(n); 
                } 
            } 
 
            [Category("Set Operators")] 
            [Description("This sample uses Intersect to create one sequence that contains the common first letter " + 
                         "from both product and customer names.")] 
            public void Linq51() 
            { 
                List<Product> products = GetProductList(); 
                List<Customer> customers = GetCustomerList(); 
 
                var productFirstChars = 
                    from prod in products 
                    select prod.ProductName[0]; 
                var customerFirstChars = 
                    from cust in customers 
                    select cust.CompanyName[0]; 
 
                var commonFirstChars = productFirstChars.Intersect(customerFirstChars); 
 
                Console.WriteLine("Common first letters from Product names and Customer names:"); 
                foreach (var ch in commonFirstChars) 
                { 
                    Console.WriteLine(ch); 
                } 
            } 
 
            [Category("Set Operators")] 
            [Description("This sample uses Except to create a sequence that contains the values from numbersA" + 
                         "that are not also in numbersB.")] 
            public void Linq52() 
            { 
                int[] numbersA = { 0245689 }; 
                int[] numbersB = { 13578 }; 
 
                IEnumerable<int> aOnlyNumbers = numbersA.Except(numbersB); 
 
                Console.WriteLine("Numbers in first array but not second array:"); 
                foreach (var n in aOnlyNumbers) 
                { 
                    Console.WriteLine(n); 
                } 
            } 
 
            [Category("Set Operators")] 
            [Description("This sample uses Except to create one sequence that contains the first letters " + 
                         "of product names that are not also first letters of customer names.")] 
            public void Linq53() 
            { 
                List<Product> products = GetProductList(); 
                List<Customer> customers = GetCustomerList(); 
 
                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); 
 
                Console.WriteLine("First letters from Product names, but not from Customer names:"); 
                foreach (var ch in productOnlyFirstChars) 
                { 
                    Console.WriteLine(ch); 
                } 
            } 
 
            public List<Product> GetProductList() 
            { 
                if (productList == null) 
                    createLists(); 
 
                return productList; 
            } 
 
            public List<Customer> GetCustomerList() 
            { 
                if (customerList == null) 
                    createLists(); 
 
                return customerList; 
            } 
 
            private void createLists() 
            { 
                // Product data created in-memory using collection initializer: 
                productList = 
                    new List<Product> { 
                    new Product { ProductID = 1, ProductName = "Chai", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 39 }, 
                    new Product { ProductID = 2, ProductName = "Chang", Category = "Beverages", UnitPrice = 19.0000M, UnitsInStock = 17 }, 
                    new Product { ProductID = 3, ProductName = "Aniseed Syrup", Category = "Condiments", UnitPrice = 10.0000M, UnitsInStock = 13 }, 
                    new Product { ProductID = 4, ProductName = "Chef Anton's Cajun Seasoning", Category = "Condiments", UnitPrice = 22.0000M, UnitsInStock = 53 }, 
                    new Product { ProductID = 5, ProductName = "Chef Anton's Gumbo Mix", Category = "Condiments", UnitPrice = 21.3500M, UnitsInStock = 0 }
                }; 
 
                // Customer/Order data read into memory from XML file using XLinq: 
                customerList = ( 
                    from e in XDocument.Load("Customers.xml"). 
                              Root.Elements("customer") 
                    select new Customer 
                    { 
                        CustomerID = (string)e.Element("id"), 
                        CompanyName = (string)e.Element("name"), 
                        Address = (string)e.Element("address"), 
                        City = (string)e.Element("city"), 
                        Region = (string)e.Element("region"), 
                        PostalCode = (string)e.Element("postalcode"), 
                        Country = (string)e.Element("country"), 
                        Phone = (string)e.Element("phone"), 
                        Fax = (string)e.Element("fax"), 
                        Orders = ( 
                            from o in e.Elements("orders").Elements("order") 
                            select new Order 
                            { 
                                OrderID = (int)o.Element("id"), 
                                OrderDate = (DateTime)o.Element("orderdate"), 
                                Total = (decimal)o.Element("total") 
                            }) 
                            .ToArray() 
                    }) 
                    .ToList(); 
            } 
        } 
    } 
} 

How to find a string within a jQuery or javascript string

Sometimes, you required to find a the existence of a small string with in a string. This article will  demonstarte , how could you do by...