Guest User

Untitled

a guest
Jan 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. /*
  2.  
  3. LINQ has a bunch of KEY types
  4.  
  5. two most notable ones are the SELECT and WHERE.
  6.  
  7. */
  8.  
  9. // WHERE conditions
  10.  
  11. List<int> myList = new List<int>();
  12.  
  13. myList.AddRange(Enumerable.Range(1,100));
  14. List<int> myList2 = new List<int>();
  15.  
  16. myList2.AddRange(Enumerable.Range(1, 50));
  17. var divisby2 = myList.Where(
  18. a => a % 2 == 0
  19. ).ToList();
  20.  
  21.  
  22. foreach (object x in divisby2)
  23. Console.WriteLine($"val divis by 2 : {x}");
  24.  
  25.  
  26. // SELECT execute function on each element
  27.  
  28.  
  29. var divisby5 = from x in myList
  30. where x % 5 == 0 select x;
  31.  
  32.  
  33. foreach (object x in divisby5)
  34. Console.WriteLine($"val divis by 5 : {x}");
  35.  
  36. var match = from x in myList
  37. join y in myList2 on x equals y
  38.  
  39. select x;
  40.  
  41.  
  42. foreach (object x in match)
  43. Console.WriteLine($"val is the same: {x}");
  44.  
  45.  
  46. //distinct
  47.  
  48. List<int> notdistinct = new List<int>()
  49. {
  50. 1,2,3,3,3,3,6,7,8,9,10
  51. };
  52.  
  53. Console.WriteLine("------------------");
  54.  
  55. foreach (object x in notdistinct)
  56. Console.WriteLine($"Elements of not distinct: {x}");
  57.  
  58.  
  59. var nowdistinct = notdistinct.Distinct();
  60.  
  61. foreach (object x in nowdistinct)
  62. Console.WriteLine($"Elements of distinct: {x}");
  63.  
  64. Console.WriteLine("------------------");
  65.  
  66.  
  67. var sqlDistinct = from x in notdistinct.Distinct()
  68. select x;
Add Comment
Please, Sign In to add comment