Advertisement
fcamuso

Linq A

Nov 16th, 2021
926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Linq_A
  6. {
  7.   class Program
  8.   {
  9.     static void Main(string[] args)
  10.     {
  11.       //giochi è una sequenza di elementi stringhe
  12.       string[] giochi = { "Borderlands 1", "Borderlands 2", "Borderlands 3", "Destiny 1", "Destiny 2", "Doom" };
  13.  
  14.       //il risultato è una sequenza (IEnumerable)
  15.       //sintassi fluente (fluent)
  16.       var sequenzaRisultato = giochi.Where(gioco => gioco.StartsWith("Bord"));
  17.  
  18.       //method sintax, query expression sintax
  19.       var ris2 = from gioco in giochi where gioco.Contains("Borderlands") select gioco;
  20.  
  21.       sequenzaRisultato = giochi
  22.         .Where(gioco => gioco.StartsWith("Bord"))
  23.         .OrderBy(gioco => gioco.Length)
  24.         .Select(gioco => gioco.ToUpper());
  25.                
  26.  
  27.       //il risultato in quanto IEnumerable è "consumabile" con un foreach
  28.       foreach (string gioco in sequenzaRisultato) Console.WriteLine(gioco);
  29.       //foreach (string gioco in ris2) Console.WriteLine(gioco);
  30.  
  31.  
  32.     }
  33.   }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement