Advertisement
kanagara

LINQ Method syntax

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