In this video I’ll cover LINQ. Language Integrated Query (LINQ) provides many tools for working with data. LINQ is similar to SQL, but it can work with data aside from databases. I’ll cover From, Where, Orderby, Select, In, Inner Joins, Group Inner Join, Equals and using LINQ with multiple different collections.
For best results take notes on the cheat sheet provided above as you watch and leave any questions you have.
If you like videos like this consider donating a $1 on Patreon.
[googleplusone]
Code & Transcript
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
// ---------- Animal.cs ---------- namespace CSharpTutA.cs { class Animal { public string Name { get; set; } public double Weight { get; set; } public double Height { get; set; } public int AnimalID { get; set; } public Animal(string name = "No Name", double weight = 0, double height = 0) { Name = name; Weight = weight; Height = height; } public override string ToString() { return string.Format("{0} weighs {1}lbs and is {2} inches tall", Name, Weight, Height); } } } // ---------- Owner.cs ---------- namespace CSharpTutA.cs { class Owner { public string Name { get; set; } public int OwnerID { get; set; } } } // ---------- Program.cs ---------- using System; using System.Linq; using System.Collections; using System.Collections.Generic; namespace CSharpTutA.cs { // Language Integrated Query (LINQ) provides // many tools for working with data // LINQ is similar to SQL, but it can work // with data aside from databases // You manipulate data using Query Expressions class Program { static void Main(string[] args) { QueryStringArray(); // Get the array returned and print it int[] intArray = QueryIntArray(); foreach(int i in intArray) Console.WriteLine(i); Console.WriteLine(); QueryArrayList(); QueryCollection(); QueryAnimalData(); Console.ReadLine(); } static void QueryStringArray() { string[] dogs = {"K 9", "Brian Griffin", "Scooby Doo", "Old Yeller", "Rin Tin Tin", "Benji", "Charlie B. Barkin", "Lassie", "Snoopy"}; // Get strings with spaces and put in // alphabetical order // from states where data comes from and // where to store each piece as you cycle // where defines conditions that must be met // orderby defines what data is used for // ordering results (ascending / descending) // select defines the variable that is // checked against the condition var dogSpaces = from dog in dogs where dog.Contains(" ") orderby dog descending select dog; foreach(var i in dogSpaces) { Console.WriteLine(i); } Console.WriteLine(); } static int[] QueryIntArray() { int[] nums = { 5, 10, 15, 20, 25, 30, 35 }; // Get numbers bigger then 20 var gt20 = from num in nums where num > 20 orderby num select num; foreach(var i in gt20) { Console.WriteLine(i); } Console.WriteLine(); // The type is an enumerable Console.WriteLine($"Get Type : {gt20.GetType()}"); // You can convert it into a List or array var listGT20 = gt20.ToList<int>(); var arrayGT20 = gt20.ToArray(); // If you change the data the query // automatically updates nums[0] = 40; foreach (var i in gt20) { Console.WriteLine(i); } Console.WriteLine(); return arrayGT20; } static void QueryArrayList() { ArrayList famAnimals = new ArrayList() { new Animal{Name = "Heidi", Height = .8, Weight = 18}, new Animal { Name = "Shrek", Height = 4, Weight = 130 }, new Animal { Name = "Congo", Height = 3.8, Weight = 90 } }; // You have to convert the ArrayList into // an IEnumerable var famAnimalEnum = famAnimals.OfType<Animal>(); var smAnimals = from animal in famAnimalEnum where animal.Weight <= 90 orderby animal.Name select animal; foreach(var animal in smAnimals) { Console.WriteLine("{0} weighs {1}lbs", animal.Name, animal.Weight); } Console.WriteLine(); } static void QueryCollection() { var animalList = new List<Animal>() { new Animal{Name = "German Shepherd", Height = 25, Weight = 77}, new Animal{Name = "Chihuahua", Height = 7, Weight = 4.4}, new Animal{Name = "Saint Bernard", Height = 30, Weight = 200} }; var bigDogs = from dog in animalList where (dog.Weight > 70) && (dog.Height > 25) orderby dog.Name select dog; foreach(var dog in bigDogs) { Console.WriteLine("A {0} weighs {1}lbs", dog.Name, dog.Weight); } Console.WriteLine(); } static void QueryAnimalData() { Animal[] animals = new[] { new Animal{Name = "German Shepherd", Height = 25, Weight = 77, AnimalID = 1}, new Animal{Name = "Chihuahua", Height = 7, Weight = 4.4, AnimalID = 2}, new Animal{Name = "Saint Bernard", Height = 30, Weight = 200, AnimalID = 3}, new Animal{Name = "Pug", Height = 12, Weight = 16, AnimalID = 1}, new Animal{Name = "Beagle", Height = 15, Weight = 23, AnimalID = 2} }; Owner[] owners = new[] { new Owner{Name = "Doug Parks", OwnerID = 1}, new Owner{Name = "Sally Smith", OwnerID = 2}, new Owner{Name = "Paul Brooks", OwnerID = 3} }; // Remove name and height var nameHeight = from a in animals select new { a.Name, a.Height }; // Convert to an object array Array arrNameHeight = nameHeight.ToArray(); foreach (var i in arrNameHeight) { Console.WriteLine(i.ToString()); } Console.WriteLine(); // Create an inner join // Join info in owners and animals using // equal values for IDs // Store values for animal and owner var innerJoin = from animal in animals join owner in owners on animal.AnimalID equals owner.OwnerID select new { OwnerName = owner.Name, AnimalName = animal.Name }; foreach (var i in innerJoin) { Console.WriteLine("{0} owns {1}", i.OwnerName, i.AnimalName); } Console.WriteLine(); // Create a group inner join // Get all animals and put them in a // newly created owner group if their // IDs match the owners ID var groupJoin = from owner in owners orderby owner.OwnerID join animal in animals on owner.OwnerID equals animal.AnimalID into ownerGroup select new { Owner = owner.Name, Animals = from owner2 in ownerGroup orderby owner2.Name select owner2 }; int totalAnimals = 0; foreach (var ownerGroup in groupJoin) { Console.WriteLine(ownerGroup.Owner); foreach (var animal in ownerGroup.Animals) { totalAnimals++; Console.WriteLine("* {0}", animal.Name); } } } } } |
Leave a Reply