Kód v c#:
using System; using System.Linq; namespace Projectik.eu { class Program { static void Main(string[] args) { // An input data array. int[] array = {1,2,3,4,5}; // ... The Select method changes each element in the result. var result = array.Select(element => element.ToString()); // Display the result. foreach (string value in result) { Console.WriteLine(value); } Console.ReadKey(); } } }
Output :
1 2 3 4 5
Teraz urobíme opačný proces. Prevedieme pole stringov na pole čísel (string[] -> int[])
Kód v c# :
using System; using System.Linq; namespace Projectik.eu { class Program { static void Main(string[] args) { // An input data array. string[] array = { "1", "2", "3", "4", "5", }; // ... The Select method changes each element in the result. var result = array.Select(element => Convert.ToInt32(element)); // Display the result. foreach (int value in result) { Console.WriteLine("{0}",value); } Console.ReadKey(); } } }
Output :
1 2 3 4 5
Pomocou metódy Where v C# filtrujeme určité prvky pola. Môžeme ju použiť v rôznych "collection types".
V prvom príklade si ukážeme, ako vybrať iba členov, ktorí maju vek nad 10 rokov. V druhom príklade si ukážeme použite v kombinácii s metódou select a vyberieme iba mena užívateľov s vekom nad 10 rokov.
Kód v c#:
using System; using System.Linq; using System.Collections.Generic; namespace Projectik.eu { class member { public member(string name,int age) { this.Name = name; this.Age = age; } public string Name {get;set; } public int Age{get;set;} } class Program { static void Main(string[] args) { // An input data array. List<member> myList = new List<member>(); //Add to list myList.Add(new member("Ferko", 15)); myList.Add(new member("Jarko", 6)); myList.Add(new member("Lucka", 12)); // ... The Where method find each element > 10 var result =myList.Where(member1 => member1.Age > 10); // Display the result. foreach (member value in result) { Console.WriteLine("{0}",value.Name); } Console.ReadKey(); } } }
Output :
Ferko
Lucka
Kód v c#:
using System; using System.Linq; using System.Collections.Generic; namespace Projectik.eu { class member { public member(string name,int age) { this.Name = name; this.Age = age; } public string Name {get;set; } public int Age{get;set;} } class Program { static void Main(string[] args) { // An input data array. List<member> myList = new List<member>(); //Add to list myList.Add(new member("Ferko", 15)); myList.Add(new member("Jarko", 6)); myList.Add(new member("Lucka", 12)); // ... The Where method find each element > 10 && only Name var result = myList.Where(member1 => member1.Age > 10).Select(member1 => member1.Name); // Display the result. foreach (string value in result) { Console.WriteLine("{0}",value); //change !!! } Console.ReadKey(); } } }
Output :
Ferko
Lucka
Kód v c# :
using System; using System.Linq; using System.Collections.Generic; namespace Projectik.eu { class Program { static void Main(string[] args) { // // input array // string[] array = { "jarko", "lucka", "ferko","projectik" }; // // Use Where method // var result1 = array.Where(item => item.Contains("a")); foreach (string value in result1) { Console.WriteLine(value); } Console.ReadKey(); } } }
Output :
Jarko
Lucka