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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BasicLinqSyntax { class Program { static void Main(string[] args) { ProductInfo[] itemsInStock = //new[] { new ProductInfo {Name="Mac's Coffee", Description="CoffeeWithTeeth",NumberInStock=24 }, new ProductInfo {Name="Milk Maid Milk", Description="Milk cows love",NumberInStock=100 }, new ProductInfo {Name="Pure Silk Tofu", Description="Bland is Possible",NumberInStock=120 }, new ProductInfo {Name="CruchyPops", Description="Cheesy, peppery goodness",NumberInStock=2 }, new ProductInfo {Name="Rip Off Water", Description="From the Tap to Your vallet",NumberInStock=100 }, new ProductInfo {Name="Classic Valpo Pizza", Description="Everyone loves pizza",NumberInStock=73 } }; SelectEveryThing(itemsInStock); SelectProductNames(itemsInStock); SelectWhereExample(itemsInStock); ProectionOfNewDataType(itemsInStock); GetCountFromQuery(itemsInStock); OrderByQuery(itemsInStock); Array objs = ProectionOfNewDataTypeReturn(itemsInStock); // with return foreach (object o in objs) { Console.WriteLine(o); } DisplayDiff(); DisplayInterSection(); DisplayUnion(); DisplayConcat(); DisplayConcatDistinct(); Console.ReadLine(); } static void SelectEveryThing(ProductInfo[] products) { Console.WriteLine("select all"); var all = from p in products select p; foreach (var p in all) Console.WriteLine(p); Console.WriteLine(" "); } static void SelectProductNames(ProductInfo[] products) { Console.WriteLine("Only product names:"); var names = from p in products select p.Name; foreach (var p in names) Console.WriteLine(p); Console.WriteLine(" "); } static void SelectWhereExample(ProductInfo[] products) { Console.WriteLine("SelectWhereExample:"); var subset = from p in products where p.NumberInStock > 25 && p.Name.Length > 10 select p; foreach (var p in subset) Console.WriteLine(p); Console.WriteLine(" "); } static void ProectionOfNewDataType(ProductInfo[] products) { Console.WriteLine("ProectionOfNewDataType - will create new objects:"); var subset = from p in products select new { p.Name, p.Description }; foreach (var p in subset) Console.WriteLine(p); // return subset; // << will not compile Console.WriteLine(" "); } static Array ProectionOfNewDataTypeReturn(ProductInfo[] products) { Console.WriteLine("ProectionOfNewDataTypeReturn - will create new objects:"); var subset = from p in products select new { p.Name, p.Description }; foreach (var p in subset) Console.WriteLine(p); Console.WriteLine(" "); return subset.ToArray(); // << will not compile } static void GetCountFromQuery(ProductInfo[] products) { int count = (from p in products select p).Count<ProductInfo>(); Console.WriteLine("Count in query = " + count); Console.WriteLine(" "); } static void ReverseSubsetQuery(ProductInfo[] products) { Console.WriteLine("select all and reverse"); var all = from p in products select p; foreach (var p in all.Reverse()) Console.WriteLine(p); Console.WriteLine(" "); } static void OrderByQuery(ProductInfo[] products) { Console.WriteLine("OrderBy Query"); var all = from p in products orderby p.Name ascending select p; foreach (var p in all.Reverse()) Console.WriteLine(p); Console.WriteLine(" "); } static void DisplayDiff() { List<string> myCars = new List<string>() { "Yugo", "Aztec", "BMW" }; List<string> yourCars = new List<string>() { "BMW", "SAAB", "Aztec" }; var carDiff = (from c in myCars select c).Except(from c2 in yourCars select c2); Console.WriteLine("Here is what you don't have but I do:"); foreach (var s in carDiff) Console.WriteLine(s); } static void DisplayInterSection() { List<string> myCars = new List<string>() { "Yugo", "Aztec", "BMW" }; List<string> yourCars = new List<string>() { "BMW", "SAAB", "Aztec" }; var carIntersect = (from c in myCars select c).Intersect(from c2 in yourCars select c2); Console.WriteLine("Here is what we have in intersection:"); foreach (var s in carIntersect) Console.WriteLine(s); } static void DisplayUnion() { List<string> myCars = new List<string>() { "Yugo", "Aztec", "BMW" }; List<string> yourCars = new List<string>() { "BMW", "SAAB", "Aztec" }; var carUnion = (from c in myCars select c).Union(from c2 in yourCars select c2); Console.WriteLine("Here is what we have in Union:"); foreach (var s in carUnion) Console.WriteLine(s); } static void DisplayConcat() { List<string> myCars = new List<string>() { "Yugo", "Aztec", "BMW" }; List<string> yourCars = new List<string>() { "BMW", "SAAB", "Aztec" }; var carConcat = (from c in myCars select c).Concat(from c2 in yourCars select c2); Console.WriteLine("Here is what we have in Concat:"); foreach (var s in carConcat) Console.WriteLine(s); } static void DisplayConcatDistinct() { List<string> myCars = new List<string>() { "Yugo", "Aztec", "BMW" }; List<string> yourCars = new List<string>() { "BMW", "SAAB", "Aztec" }; var carConcat = (from c in myCars select c).Concat(from c2 in yourCars select c2); Console.WriteLine("Here is what we have in ConcatDistinct:"); foreach (var s in carConcat.Distinct()) Console.WriteLine(s); } static void AggregateOps() { double[] winterTemps = { 2.0, -21.3, 8, -4, 0, 8.2 }; Console.WriteLine("Max temp is {0}", (from t in winterTemps select t).Max()); Console.WriteLine("Min temp is {0}", (from t in winterTemps select t).Min()); Console.WriteLine("Average temp is {0}", (from t in winterTemps select t).Average()); Console.WriteLine("Sum temp is {0}", (from t in winterTemps select t).Sum()); } } class ProductInfo { public string Name { get; set; } public string Description { get; set; } public int NumberInStock { get; set; } public override string ToString() { return string.Format("Name={0},Description={1},Number in Stock={2}", Name, Description, NumberInStock); } } } |