Advertisement
karlakmkj

LINQ - query & method syntax 2

Nov 27th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.84 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 1: from - where - select query
  14.       var heroesWithI = from hero in heroes
  15.           where hero.Contains("i")
  16.           select hero;
  17.       // Method 2: Method query using lambda - Where()
  18.       //var heroesWithI = heroes.Where(hero => hero.Contains("i"));
  19.        
  20.       foreach (string s in heroesWithI)
  21.       {
  22.       Console.WriteLine(s);
  23.       }
  24.  
  25.       //from - select query
  26.       var underscored = from hero in heroes
  27.           select hero.Replace(" ", "_");
  28.          
  29.       foreach (string s in underscored)
  30.       {
  31.       Console.WriteLine(s);
  32.       }
  33.  
  34.     }
  35.   }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement