Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- LINQ has a bunch of KEY types
- two most notable ones are the SELECT and WHERE.
- */
- // WHERE conditions
- List<int> myList = new List<int>();
- myList.AddRange(Enumerable.Range(1,100));
- List<int> myList2 = new List<int>();
- myList2.AddRange(Enumerable.Range(1, 50));
- var divisby2 = myList.Where(
- a => a % 2 == 0
- ).ToList();
- foreach (object x in divisby2)
- Console.WriteLine($"val divis by 2 : {x}");
- // SELECT execute function on each element
- var divisby5 = from x in myList
- where x % 5 == 0 select x;
- foreach (object x in divisby5)
- Console.WriteLine($"val divis by 5 : {x}");
- var match = from x in myList
- join y in myList2 on x equals y
- select x;
- foreach (object x in match)
- Console.WriteLine($"val is the same: {x}");
- //distinct
- List<int> notdistinct = new List<int>()
- {
- 1,2,3,3,3,3,6,7,8,9,10
- };
- Console.WriteLine("------------------");
- foreach (object x in notdistinct)
- Console.WriteLine($"Elements of not distinct: {x}");
- var nowdistinct = notdistinct.Distinct();
- foreach (object x in nowdistinct)
- Console.WriteLine($"Elements of distinct: {x}");
- Console.WriteLine("------------------");
- var sqlDistinct = from x in notdistinct.Distinct()
- select x;
Add Comment
Please, Sign In to add comment