Advertisement
karlakmkj

LINQ - query & method syntax

Nov 27th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 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.       // Query syntax
  14.       var queryResult = from x in heroes
  15.                         where x.Contains("a")
  16.                         select $"{x} contains an 'a'";
  17.      
  18.       // Method syntax
  19.       var methodResult = heroes
  20.         .Where(x => x.Contains("a"))
  21.         .Select(x => $"{x} contains an 'a'");
  22.      
  23.       // Printing...
  24.       Console.WriteLine("queryResult:");
  25.       foreach (string s in queryResult)
  26.       {
  27.         Console.WriteLine(s);
  28.       }
  29.      
  30.       Console.WriteLine("\nmethodResult:");
  31.       foreach (string s in methodResult)
  32.       {
  33.         Console.WriteLine(s);
  34.       }
  35.     }
  36.   }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement