Advertisement
karlakmkj

LINQ - Method syntax

Nov 27th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace LearnLinq
  6. {
  7.   class Program
  8.   {
  9.     static void Main()
  10.     {
  11.       string[] heroes = { "D. Va", "Lucio", "Mercy", "Soldier 76", "Pharah", "Reinhardt" };
  12.  
  13.       //Method syntax using separate statements
  14.       var HeroesWithC = heroes.Where( h => h.Contains("c"));
  15.       var lowerHeroesWithC = HeroesWithC.Select( h=> h.ToLower());
  16.  
  17.       //Method syntax using chain expressions
  18.       var sameResult = heroes
  19.           .Where(h => h.Contains("c"))
  20.           .Select(h=> h.ToLower());  
  21.  
  22.       //Both ways will print the same result
  23.       foreach (string s in lowerHeroesWithC)
  24.       {
  25.       Console.WriteLine(s);
  26.       }    
  27.  
  28.       foreach (string s in sameResult)
  29.       {
  30.       Console.WriteLine(s);
  31.       }
  32.  
  33.     }
  34.   }
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement