123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace linqSimpleArray { class Program { static void Main(string[] args) { QueryOverStrings(); Console.ReadLine(); } static void QueryOverStrings() { string[] games = { "Morrowwind", "Uncharted 2", "Fallout 3", "Daxter", "System Shock 2" }; // get games only with spaces inside IEnumerable<string> subset = from g in games where g.Contains(" ") orderby g select g; // the same through implicit tipization var subset2 = from g in games where g.Contains(" ") orderby g select g; // linq operator will be implemented here foreach (string s in subset) Console.WriteLine("Item: {0}", s); Console.WriteLine("123"); foreach (string s in subset2) Console.WriteLine("Item: {0}", s); //------------- immediate execution of linq int[] numbers = { 10,20,30,40,1,2,3 }; int[] subarray = (from i in numbers where i<10 select i).ToArray<int>(); // executed here foreach (int j in subarray) Console.WriteLine("Item: {0}", j); // or //for (int j = 0; j < subarray.Length; j++) { Console.WriteLine("Item: {0}", subarray[j]); } LinqReturnExamples.GetStringSubset(); LinqReturnExamples.GetStringSubsetAsArray(); } } class LinqReturnExamples { public static IEnumerable<string> GetStringSubset() { string[] games = { "Morrowwind", "Uncharted 2", "Fallout 3", "Daxter", "System Shock 2" }; // get games only with spaces inside IEnumerable<string> subset = from g in games where g.Contains(" ") orderby g select g; return subset; } public static string[] GetStringSubsetAsArray() { string[] games = { "Morrowwind", "Uncharted 2", "Fallout 3", "Daxter", "System Shock 2" }; // get games only with spaces inside string[] subarray = (from g in games where g.Length < 10 select g).ToArray<string>(); // executed here return subarray; } } } |