Created
September 17, 2019 09:02
-
-
Save lavantien/d643d9c446f4db6d7117d97de7727e53 to your computer and use it in GitHub Desktop.
Basic ways to use LINQ on collections.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Collections; | |
namespace TestCS | |
{ | |
class Program | |
{ | |
static void Main(string[] args) { | |
QueryStringArray(); | |
QueryIntArray(); | |
QuerryArrayList(); | |
QuerryCollection(); | |
QuerryAnimalData(); | |
Console.ReadKey(); | |
} | |
static void QueryStringArray() { | |
string[] dogs = { "K 9", "Brian Griffin", "Scooby Doo", "Old Yeller", "Rin Tin Tin", "Benji", "Charlie B. Barkin", "Lassie", "Snoopy" }; | |
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 }; | |
var gt20 = from num in nums | |
where num > 20 | |
orderby num | |
select num; | |
foreach (var i in gt20) { | |
Console.WriteLine(i); | |
} | |
Console.WriteLine(); | |
Console.WriteLine($"Get Type : {gt20.GetType()}"); | |
var listGT20 = gt20.ToList<int>(); | |
var arrayGT20 = gt20.ToArray(); | |
nums[0] = 40; | |
foreach (var i in gt20) { | |
Console.WriteLine(i); | |
} | |
Console.WriteLine(); | |
return arrayGT20; | |
} | |
static void QuerryArrayList() { | |
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 | |
} | |
}; | |
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(animal); | |
} | |
Console.WriteLine(); | |
} | |
static void QuerryCollection() { | |
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(dog); | |
} | |
Console.WriteLine(); | |
} | |
static void QuerryAnimalData() { | |
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 | |
} | |
}; | |
var nameHeight = from a in animals | |
select new { a.Name, a.Height }; | |
Array arrNameHeight = nameHeight.ToArray(); | |
foreach (var i in arrNameHeight) { | |
Console.WriteLine(i.ToString()); | |
} | |
Console.WriteLine(); | |
var innerJoin = from animal in animals | |
join owner in owners | |
on animal.AnimalID equals owner.OwnerID | |
orderby owner.Name | |
select new { | |
OwnerName = owner.Name, | |
AnimalName = animal.Name | |
}; | |
foreach (var i in innerJoin) { | |
Console.WriteLine("{0} owns {1}", i.OwnerName, i.AnimalName); | |
} | |
Console.WriteLine(); | |
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 | |
}; | |
foreach (var ownerGroup in groupJoin) { | |
Console.WriteLine(ownerGroup.Owner); | |
foreach (var animal in ownerGroup.Animals) { | |
Console.WriteLine("* {0}", animal.Name); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment