Csharp/C Sharp/LINQ/select
Содержание
- 1 Converting an Array of Strings to Integers
- 2 Converting an Array of Strings to Integers and Sorting It
- 3 Demonstrating the Query Results Changing Between Enumerations
- 4 Query Reuse
- 5 Query with Intentional Exception Deferred Until Enumeration
- 6 Returning a List
- 7 Select array item by type
- 8 Select with Anonymous Types: iterates over each element to print the element"s name
- 9 Select with Anonymous Types: prints the name of every product in the product list along with the category of the product and its unit price
- 10 Select with Anonymous Types: prints uppercase and lowercase versions of each string in an input array
- 11 Transformation: link two array.
- 12 Use LINQ to query characters in a string
- 13 Use LINQ with Dictionary
- 14 uses select to create a sequence of each product name.
Converting an Array of Strings to Integers
<source lang="csharp"> using System; using System.Linq; public class MainClass {
public static void Main() { string[] numbers = { "0042", "010", "9", "2q7" }; int[] nums = numbers.Select(s => Int32.Parse(s)).ToArray(); }
}
</source>
Converting an Array of Strings to Integers and Sorting It
<source lang="csharp"> using System; using System.Linq; public class MainClass {
public static void Main() { string[] numbers = { "0042", "010", "9", "2q7" }; int[] nums = numbers.Select(s => Int32.Parse(s)).OrderBy(s => s).ToArray(); foreach (int num in nums) Console.WriteLine(num); }
}
</source>
Demonstrating the Query Results Changing Between Enumerations
<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass {
public static void Main() { int[] intArray = new int[] { 1, 2, 3 }; IEnumerable<int> ints = intArray.Select(i => i); foreach (int i in ints) Console.WriteLine(i); intArray[0] = 5; foreach (int i in ints) Console.WriteLine(i); }
}
</source>
Query Reuse
<source lang="csharp"> using System; using System.Linq; static class QueryReuse {
static double Square(double n) { Console.WriteLine("Computing Square(" + n + ")..."); return Math.Pow(n, 2); } public static void Main() { int[] numbers = { 1, 2, 3 }; var query = from n in numbers select Square(n); foreach (var n in query) Console.WriteLine(n); for (int i = 0; i < numbers.Length; i++) numbers[i] = numbers[i] + 10; foreach (var n in query) Console.WriteLine(n); }
}
</source>
Query with Intentional Exception Deferred Until Enumeration
<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass {
public static void Main() { string[] strings = { "one", "two", null, "three" }; Console.WriteLine("Before Where() is called."); IEnumerable<string> ieStrings = strings.Where(s => s.Length == 3); Console.WriteLine("After Where() is called."); foreach (string s in ieStrings) { Console.WriteLine("Processing " + s); } }
}
</source>
Returning a List
<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass {
public static void Main() { int[] intArray = new int[] { 1, 2, 3 }; List<int> ints = intArray.Select(i => i).ToList(); foreach (int i in ints) Console.WriteLine(i); intArray[0] = 5; foreach (int i in ints) Console.WriteLine(i); }
}
</source>
Select array item by type
<source lang="csharp"> using System; using System.Linq; static class TestArray {
static void Main() { Object[] array = { "String", 12, true, "a" }; var types = array .Select(item => item.GetType().Name) .OrderBy(type => type); }
}
</source>
Select with Anonymous Types: iterates over each element to print the element"s name
<source lang="csharp"> using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MainClass {
public static void Main() { int[] numbers = { 5, 4, 1}; string[] strings = { "zero", "one", "two"}; var digitOddEvens = from n in numbers select new { Digit = strings[n], Even = (n % 2 == 0) }; foreach (var d in digitOddEvens) { Console.WriteLine("The digit {0} is {1}.", d.Digit, d.Even ? "even" : "odd"); } }
}
</source>
Select with Anonymous Types: prints the name of every product in the product list along with the category of the product and its unit price
<source lang="csharp"> using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MainClass {
public static void Main() { List<Product> products = GetProductList(); var productInfos = from p in products select new { p.ProductName, p.Category, Price = p.UnitPrice }; Console.WriteLine("Product Info:"); foreach (var productInfo in productInfos) { Console.WriteLine("{0} is in the category {1} and costs {2} per unit.", productInfo.ProductName, productInfo.Category, productInfo.Price); } } static List<Product> GetProductList() { List<Product> empTree = new List<Product>(); empTree.Add(new Product { ProductName = "A", Category = "O", UnitPrice = 12, UnitsInStock = 5, Total = 36, OrderDate = new DateTime(2005, 1, 1), Id = 1 }); empTree.Add(new Product { ProductName = "B", Category = "O", UnitPrice = 2, UnitsInStock = 4, Total = 35, OrderDate = new DateTime(2005, 1, 1), Id = 1 }); empTree.Add(new Product { ProductName = "C", Category = "O", UnitPrice = 112, UnitsInStock = 3, Total = 34, OrderDate = new DateTime(2005, 1, 1), Id = 1 }); empTree.Add(new Product { ProductName = "D", Category = "O", UnitPrice = 112, UnitsInStock = 0, Total = 33, OrderDate = new DateTime(2005, 1, 1), Id = 1 }); empTree.Add(new Product { ProductName = "E", Category = "O", UnitPrice = 1112, UnitsInStock = 2, Total = 32, OrderDate = new DateTime(2005, 1, 1), Id = 1 }); empTree.Add(new Product { ProductName = "F", Category = "O", UnitPrice = 11112, UnitsInStock = 0, Total = 31, OrderDate = new DateTime(2005, 1, 1), Id = 1 }); return empTree; }
} class Product : IComparable<Product> {
public string ProductName { get; set; } public string Category { get; set; } public int UnitPrice { get; set; } public int UnitsInStock { get; set; } public int Total { get; set; } public DateTime OrderDate { get; set; } public int Id { get; set; } public override string ToString() { return String.Format("Id: {0}, Name: {1} , Category: {3}", this.Id, this.ProductName, this.Category); } int IComparable<Product>.rupareTo(Product other) { if (other == null) return 1; if (this.Id > other.Id) return 1; if (this.Id < other.Id) return -1; return 0; }
}
</source>
Select with Anonymous Types: prints uppercase and lowercase versions of each string in an input array
<source lang="csharp"> using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MainClass {
public static void Main() { string[] words = { "Abc", "aBc", "AAA" }; var upperLowerWords = from w in words select new { Upper = w.ToUpper(), Lower = w.ToLower() }; foreach (var ul in upperLowerWords) { Console.WriteLine("Uppercase: {0}, Lowercase: {1}", ul.Upper, ul.Lower); } }
}
</source>
Transformation: link two array.
<source lang="csharp"> using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MainClass {
public static void Main() { int[] numbers = { 5, 4, 1}; string[] strings = { "zero", "one", "two"}; var textNums = from n in numbers select strings[n]; Console.WriteLine("Number strings:"); foreach (var s in textNums) { Console.WriteLine(s); } }
}
</source>
Use LINQ to query characters in a string
<source lang="csharp"> using System; using System.Collections.Generic; using System.Linq; static class TestString {
static void Main() { var count = "abc 8" .Where(c => !Char.IsLetter(c)) .Count(); Console.WriteLine(count); }
}
</source>
Use LINQ with Dictionary
<source lang="csharp"> using System; using System.Collections.Generic; using System.Linq; static class TestDictionary {
static void Main() { Dictionary<int, string> frenchNumbers; frenchNumbers = new Dictionary<int, string>(); frenchNumbers.Add(0, "zero"); frenchNumbers.Add(1, "one"); frenchNumbers.Add(2, "two"); frenchNumbers.Add(3, "three"); frenchNumbers.Add(4, "four"); var evenFrenchNumbers = from entry in frenchNumbers where (entry.Key % 2) == 0 select entry.Value; }
}
</source>
uses select to create a sequence of each product name.
<source lang="csharp"> using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MainClass {
public static void Main() { List<Product> products = GetProductList(); var productNames = from p in products select p.ProductName; foreach (var productName in productNames) { Console.WriteLine(productName); } } static List<Product> GetProductList() { List<Product> empTree = new List<Product>(); empTree.Add(new Product { ProductName = "A", Category = "O", UnitPrice = 12, UnitsInStock = 5, Total = 36, OrderDate = new DateTime(2005, 1, 1), Id = 1 }); empTree.Add(new Product { ProductName = "B", Category = "O", UnitPrice = 2, UnitsInStock = 4, Total = 35, OrderDate = new DateTime(2005, 1, 1), Id = 1 }); empTree.Add(new Product { ProductName = "C", Category = "O", UnitPrice = 112, UnitsInStock = 3, Total = 34, OrderDate = new DateTime(2005, 1, 1), Id = 1 }); empTree.Add(new Product { ProductName = "D", Category = "O", UnitPrice = 112, UnitsInStock = 0, Total = 33, OrderDate = new DateTime(2005, 1, 1), Id = 1 }); empTree.Add(new Product { ProductName = "E", Category = "O", UnitPrice = 1112, UnitsInStock = 2, Total = 32, OrderDate = new DateTime(2005, 1, 1), Id = 1 }); empTree.Add(new Product { ProductName = "F", Category = "O", UnitPrice = 11112, UnitsInStock = 0, Total = 31, OrderDate = new DateTime(2005, 1, 1), Id = 1 }); return empTree; }
} class Product : IComparable<Product> {
public string ProductName { get; set; } public string Category { get; set; } public int UnitPrice { get; set; } public int UnitsInStock { get; set; } public int Total { get; set; } public DateTime OrderDate { get; set; } public int Id { get; set; } public override string ToString() { return String.Format("Id: {0}, Name: {1} , Category: {3}", this.Id, this.ProductName, this.Category); } int IComparable<Product>.rupareTo(Product other) { if (other == null) return 1; if (this.Id > other.Id) return 1; if (this.Id < other.Id) return -1; return 0; }
}
</source>