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 { get; set; } public string ProductName { get; set; } public string Category { get; set; } public decimal UnitPrice { get; set; } public int UnitsInStock { get; set; } } public class Order { public int OrderID { get; set; } public DateTime OrderDate { get; set; } public decimal Total { get; set; } } public class Customer { public string CustomerID { get; set; } public string CompanyName { get; set; } public string Address { get; set; } public string City { get; set; } public string Region { get; set; } public string PostalCode { get; set; } public string Country { get; set; } public string Phone { get; set; } public string Fax { get; set; } public Order[] Orders { get; set; } } 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 = { 2, 2, 3, 5, 5 }; 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 = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; 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 = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; 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 = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; 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(); } } } }
Sunday, 29 June 2014
LINQ - Set Operators
Subscribe to:
Post Comments (Atom)
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...
-
In this article i am explaining how to Read Write Edit Word Document With FileStream StreamWriter In ASP.NET . For this i've added a te...
-
Configuring LDAP in Sitefinity 4.4 Windows Active Directory is a directory service created by Microsoft. Active Directory uses a...
No comments:
Post a Comment