1. Lambda expressions
var x=Square(5);
//jak zapisac do zmiennej funkcje? - uzywamy delegaty, czyli robimy tak:
//delegata -klasa,ktora okresla typ zmiennej
//a)gotowe implementacje dla funkcji,ktore zwracaja wart
//b)gotowe impelmentacje dla funkcji typu void
Func function= //typ wejsciowy oraz typ wyjsciowy
number => number*number; //tutaj uzywamy wyrazenie lambda
Action action=
(text,number)=>
{
Console.WriteLine($"{text},{number}");
};
var x1=function(5);
public static void LogToConsole(string text,int number)
{
Console.WriteLine($"{text},{number}");
}
public static int Square(int number)
{
return number*number;
}
2.LINQ
-mechanism, which allows for operations on collections;syntax similar to SQL
-it can be used on every collection, which implements interface INumerable
-extension methods
public static class CSVReader
{
public static List ReadCarsFromCsv()
{
var text = File.ReadAllLines("../../../cars.csv");
text=text.Skip(2).ToArray(); //omijamy 2 pierwsze linijki
var carlist=new List();
foreach (var line in text)
{
var lineWithCommas=line.Replace('.',',');
string[] data=lineWithCommas.Split(';');
var car = new Car()
{
Name = data[0],
FuelEconomy=double.Parse(data[1]),
Cylinders=int.Parse(data[2]),
Displacement=double.Parse(data[3]),
Horsepower=double.Parse(date[4]),
Weight=double.Parse(data[5]),
Acceleration=double.Parse(data[6]),
ProductionYear=int.Parse(data[7]),
Origin=data[8]
} ;
carlist.Add(car);
}
return carlist;
}
}
public class Car
{
public string Name {get; set;}
public double FuelEconomy{get;set;}
public Cylinders {get;set;}
public Double Displacement {get;set;}
public double HorsePower{get;set;}
public double Wieght{get;set;}
public double Acceleration{get;set;}
public inet ProductionYear{get;set;}
public string Origin {get;set;}
}
public class CarShop
{
private List _cars;
public CarShop()
{
_cars=CSVReader.ReadCarsFromCSV();
}
//jak wyswietlic samochody, ktore maja pojemnosc silnika powyzej x
public void GetCarsWithHighHorsepower(double minimumhp)
{
var filtereCars=_cars.Where(c => c.Horsepower >= minimumhp);
_cars.Add(new Car() { Name ="test",Horsepower=100});
foreach (var car in filteredCars)
{
Console.WriteLine($"{car.HorsePower},{car.Name}");
}
}
}
-deffered execution (late execution), command is executedexactly in the moment of direct invocation
var filteredCars=_cars.Where(c=>c.Horsepower >= minimumhp).toList(); //to spowoduje, ze nie bedzie uzywane to deffered execution
//filtrowanie
public void GetCarsWithOrigin(string origin)
{
var cars = _cars.Where(c=>c.Origin == origin);
foreach(var car in cars)
{
Console.WriteLine($"{car.Origin},{car.Name});
}
}
//sortowanie
public void SortCarsByFuelEconomy(bool descending = false)
{
IEnumerable sortedCars;
if (!descending)
{
sortedCars = _cars.OrderBy(c => c.FuelEconomy);
}
else
{
sortedCars = _cars.OrderByDescending(c => c.FuelEconomy);
}
foreach (var car in sortedCars)
{
Console.Writeline($"{car.FuelEconomy}");
}
}
//jak wyswietlic top 10
sortedCars=sortedCars.Take(10);
//pobieramy samochody gdzie rok produkcji miedzy a i b,posrotujemy po roku produkcji oraz mocy silnika
public void GetCarsFromProductionYEars(int from,int to)
{
var query=_cars.Where(c=>c.ProductionYear>=from && c.ProductionYear <= to). OrderBy(c=>c.ProductionYear).
ThenBy(c=>c.Horsepower);
foreach (var car in query)
{
Console.WriteLine($"Production year: {car.ProductionYear}, horsepower: {car.Horsepower}");
}
}
//pobieramy najszybszy samochod z konkretnego roku
public void GetFastestCarFromYear(int year)
{
var fastestCar = _cars.
Where(c=>c,ProductionYear == year).
OrderBy(c=>c.Acceleration).
First(); //FirstOrDefault(); - ta metoda zwroci null jesli nie znajdzie wyniku
Console.WriteLine($"Acceleration: {fastesCar.Acceleration},name:{fastestCar.Name}");
}
//grupowanie elementow
public void GroupOrigin()
{
var query=_cars.GroupBy(c=>c.Origin);
foreach(var element in query)
{
Console.WriteLine(element.Key); //ten klucz opisuje wlasciwosc po ktorej grupowalismy
foreach(var car in element.Take(5))
{
Console.WriteLine($"{car.Origin},name: {car:Name}");
}
}
//join
_cars.Join()
//union
_cars.Union()
//query syntax w LINQ - calkowicie jak SQL wtedy wyglada
var query=from s in stringlist
where s.Contains("Tutorials")
select s;
//mozna uzywac rowniez skladni select
public void SelectSample()
{
//tutaj tworza sie obiekty anonimowe o polach:Name oraz HorsePower
var query=_cars.Select(c=>new {c.Name,c.Horsepower});
}