- class Program
- {
- static void Main(string[] args)
- {
- //測試資料
- string[] names = {"andy", "Peter", "candy", "tom"};
- //linq query expression syntax
- IEnumerable<string> filternames = names.Where(n => n.Length >= 4);
- foreach (string name in filternames)
- {
- Console.WriteLine(name);
- }
- //chaining Query Operators
- //有左到右的操作順序,篩選、排序、轉換
- IEnumerable<string> query = names
- .Where(n => n.Contains("a"))
- .OrderBy(n => n.Length)
- .Select(n => n.ToUpper());
- foreach (string name in query)
- {
- Console.WriteLine(name);
- }
- //Fluent syntax
- IEnumerable<string> filternames2 = from n in names
- where n.Contains("a")
- select n;
- foreach (string name in filternames2)
- {
- Console.WriteLine(name);
- }
- //nature ordering
- int[] numbers = {10, 9, 8, 7, 6};
- IEnumerable<int> firstthree = numbers.Take(3); // 10 9 8
- IEnumerable<int> lasttwo = numbers.Skip(3); // 7 6
- IEnumerable<int> reversed = numbers.Reverse(); // 6 7 8 9 10
- //結合陣列
- int[] seq1 = {1, 2, 3};
- int[] seq2 = {3, 4, 5};
- IEnumerable<int> concat = seq1.Concat(seq2); // 1 2 3 3 4 5
- IEnumerable<int> union = seq1.Union(seq2); // 1 2 3 4 5
- foreach (int i in concat)
- {
- Console.Write(i.ToString() + "|");
- }
- }
- }
2014年4月6日 星期日
[Linq] 基礎 part1
就...該還的基礎~
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言