pluralsight-linq-fundamentals/Features/Program.cs

80 lines
2.3 KiB
C#
Raw Normal View History

2021-02-24 11:56:17 +11:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
2021-02-24 11:56:17 +11:00
namespace Features
{
class Program
{
// Modelling a company in this project
static void Main(string[] args)
{
2021-02-24 15:40:04 +11:00
/* Generic Delegate & Lambda Expression */
Func<int, int> square = x => x*x;// Define a generic delegate functions named square for square operation
Func<int, int, int> add = (x, y) =>
{
int temp = x + y;
return temp;
};
Action<int> write = x => Console.WriteLine(x);
write(square(add(3, 5)));
/* LINQ & C# */
2021-02-24 11:56:17 +11:00
IEnumerable<Employee> developers = new Employee[]
{
new Employee {Id = 1, Name = "Scott"},
new Employee {Id = 2, Name = "Chris"}
};
List<Employee> sales = new List<Employee>()
{
new Employee {Id = 3, Name = "Alex"}
};
2021-02-24 13:49:26 +11:00
Console.WriteLine(sales.MyLinqCount());
2021-02-24 11:56:17 +11:00
var enumerator = developers.GetEnumerator();
while (enumerator.MoveNext())
{
Console.WriteLine(enumerator.Current.Name); // .Current return value of current enumerator
}
foreach (var person in developers)
{
Console.WriteLine();
}
2021-02-24 15:40:04 +11:00
foreach (var employee in developers
.Where(e => e.Name.Length == 5)
.OrderBy(e => e.Name))
{
Console.WriteLine(employee.Name);
}
// LINQ (Method syntax)
var query = developers.Where(e => e.Name.Length == 5)
.OrderBy(e => e.Name);
// LINQ (Query syntax)
var query2 = from developer in developers
where developer.Name.Length == 5
orderby developer.Name descending
select developer;
foreach (var employee in query)
{
Console.WriteLine(employee.Name);
}
foreach (var employee in query2)
{
Console.WriteLine(employee.Name);
}
2021-02-24 11:56:17 +11:00
}
}
}