Advertisement
kanagara

LINQ

May 2nd, 2021 (edited)
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. using System;
  2. //Za koriscenje LINQ potrebno je koristiti System.Linq namespace
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. public class Program
  6. {
  7.     public class Person{
  8.         public string firstName,lastName;  
  9.     }
  10.    
  11.     public class PersonAndWork{
  12.         public Person person;
  13.         public string workPlace;
  14.     }
  15.    
  16.     public static void Main()
  17.     {
  18.         List<PersonAndWork> list = new List<PersonAndWork>(){
  19.             new PersonAndWork(){person = new Person(){firstName = "Elon", lastName = "Musk"}, workPlace = "Tesla"},
  20.             new PersonAndWork(){person = new Person(){firstName = "Larry", lastName = "Page"}, workPlace = "Alphabet"},
  21.             new PersonAndWork(){person = new Person(){firstName = "Satya", lastName = "Nadella"}, workPlace = "Microsoft"},
  22.             new PersonAndWork(){person = new Person(){firstName = "Petar", lastName = "Petrovic"}, workPlace = "DressCode"},
  23.             new PersonAndWork(){person = new Person(){firstName = "Petra", lastName = "Jovanovic"}, workPlace = "DressCode"},
  24.         };
  25.        
  26.         //Trazimo autore koji rade u DressCode-u
  27.         var result = from s in list
  28.                     where s.workPlace == "DressCode"
  29.                     select s.person;
  30.         //Na ekranu ce se ispisati Petar Petrovic i Petra Jovanovic
  31.         foreach(var author in result)
  32.             Console.WriteLine($"{author.firstName} {author.lastName}");
  33.    
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement