|
@@ -1,406 +1,112 @@
|
|
|
-# LINQ
|
|
|
-# ʕ•͡-•ʔ
|
|
|
-## Основы LINQ
|
|
|
-### Выберем из массива строки, начинающиеся на определенную букву и отсортируем полученный список:
|
|
|
+## Предметная область
|
|
|
+# "University"
|
|
|
+# ૮₍ • ᴥ • ₎ა
|
|
|
+# ૮₍ • ᴥ • ₎ა
|
|
|
+# ૮₍ • ᴥ • ₎ა
|
|
|
+### OOOPS..
|
|
|
+## Создание
|
|
|
```
|
|
|
-string[] students = {"Женя", "Моисей", "Влада", "Амир", "Маша", "Катя"};
|
|
|
+namespace ClassLibrary1;
|
|
|
|
|
|
-var selectedStudents = new List<string>();
|
|
|
-
|
|
|
-foreach(string s in students)
|
|
|
+public class Student
|
|
|
{
|
|
|
- if (s.ToUpper().StartsWith("М"))
|
|
|
- selectedStudents.Add(s);
|
|
|
+ public string name { get; set; }
|
|
|
+ public int age { get; set; }
|
|
|
}
|
|
|
-
|
|
|
-selectedStudents.Sort();
|
|
|
-
|
|
|
-foreach (string s in selectedStudents)
|
|
|
- Console.WriteLine(s);
|
|
|
-```
|
|
|
-```
|
|
|
-Маша
|
|
|
-Моисей
|
|
|
-```
|
|
|
-### Теперь проведем те же действия с помощью LINQ:
|
|
|
-```
|
|
|
-string[] students = {"Женя", "Моисей", "Влада", "Амир", "Маша", "Катя"};
|
|
|
-
|
|
|
-var selectedStudents = from t in students
|
|
|
- where t.ToUpper().StartsWith("М")
|
|
|
- orderby t
|
|
|
- select t;
|
|
|
-
|
|
|
-foreach (string s in selectedStudents)
|
|
|
- Console.WriteLine(s);
|
|
|
-```
|
|
|
-```
|
|
|
-Маша
|
|
|
-Моисей
|
|
|
-```
|
|
|
-## Методы расширения LINQ
|
|
|
```
|
|
|
-string[] students = { "Женя", "Моисей", "Влада", "Амир", "Маша", "Катя" };
|
|
|
-
|
|
|
-var selectedStudents = students
|
|
|
- .Where(t=>t.ToUpper().StartsWith("М"))
|
|
|
- .OrderBy(t => t);
|
|
|
-
|
|
|
-foreach (string s in selectedStudents)
|
|
|
- Console.WriteLine(s);
|
|
|
+## Подключение
|
|
|
```
|
|
|
-```
|
|
|
-Маша
|
|
|
-Моисей
|
|
|
-```
|
|
|
-## Фильтрация
|
|
|
-### Фильтрация с помощью операторов LINQ:
|
|
|
-```
|
|
|
-using System;
|
|
|
-using System.Linq;
|
|
|
+using ClassLibrary1;
|
|
|
|
|
|
-class Program
|
|
|
-{
|
|
|
- static void Main()
|
|
|
- {
|
|
|
- int[] grades = { 1, 2, 3, 4, 5, 6, 7 };
|
|
|
+var gosha = new Student {
|
|
|
+ name = "Гоша",
|
|
|
+ age = 18 };
|
|
|
|
|
|
- var lowGrades = grades.Where(grade => grade < 5);
|
|
|
-
|
|
|
- Console.WriteLine("Оценки меньше '5':");
|
|
|
- foreach (var grade in lowGrades)
|
|
|
- {
|
|
|
- Console.WriteLine(grade);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
+Console.WriteLine(gosha.name);
|
|
|
```
|
|
|
```
|
|
|
-1
|
|
|
-2
|
|
|
-3
|
|
|
-4
|
|
|
+Гоша
|
|
|
```
|
|
|
-## Выборка сложных объектов
|
|
|
-### Создадим набор студентов и выберем из них тех, которым меньше 23 лет:
|
|
|
+## Динамическая загрузка сборок и позднее связывание
|
|
|
+### исследуем сборку на наличие в ней различных типов:
|
|
|
```
|
|
|
-using System;
|
|
|
-using System.Collections.Generic;
|
|
|
+using System.Reflection;
|
|
|
|
|
|
-class Student
|
|
|
-{
|
|
|
- public string Name { get; set; }
|
|
|
- public int Age { get; set; }
|
|
|
+var asm = Assembly.LoadFrom(
|
|
|
+ "ClassLibrary1.dll");
|
|
|
|
|
|
- public Student(string name, int age)
|
|
|
- {
|
|
|
- Name = name;
|
|
|
- Age = age;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-class Program
|
|
|
-{
|
|
|
- static void Main()
|
|
|
- {
|
|
|
- List<Student> users = new List<Student>
|
|
|
- {
|
|
|
- new Student("Влада", 23),
|
|
|
- new Student("Саня", 19),
|
|
|
- new Student("Амир", 21),
|
|
|
- new Student("Маша", 24)
|
|
|
- };
|
|
|
-
|
|
|
- var selectedUsers = users.Where(u => u.Age <23);
|
|
|
- foreach (Student user in selectedUsers)
|
|
|
- {
|
|
|
- Console.WriteLine($"{user.Name}, - {user.Age}");
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-```
|
|
|
-```
|
|
|
-Саня, - 19
|
|
|
-Амир, - 21
|
|
|
-```
|
|
|
-## Сложные фильтры
|
|
|
-### Для создания запроса применяется метод SelectMany:
|
|
|
-```
|
|
|
-using System;
|
|
|
-using System.Collections.Generic;
|
|
|
-using System.Linq;
|
|
|
+Console.WriteLine(asm.FullName);
|
|
|
|
|
|
-class Student
|
|
|
-{
|
|
|
- public string Name { get; set; }
|
|
|
- public int Age { get; set; }
|
|
|
- public List<string> Languages { get; set; }
|
|
|
-}
|
|
|
+var types = asm.GetTypes();
|
|
|
|
|
|
-class Program
|
|
|
+foreach(Type t in types)
|
|
|
{
|
|
|
- static void Main()
|
|
|
- {
|
|
|
- List<Student> users = new List<Student>
|
|
|
- {
|
|
|
- new Student { Name = "Арсен", Age = 21, Languages = new List<string> { "английский", "китайский" } },
|
|
|
- new Student { Name = "Вася", Age = 24, Languages = new List<string> { "испанский", "японский" } },
|
|
|
- new Student { Name = "Глория", Age = 23, Languages = new List<string> { "китайский", "немецкий" } },
|
|
|
- new Student { Name = "Стеша", Age = 29, Languages = new List<string> { "итальянский", "английский"} }
|
|
|
- };
|
|
|
-
|
|
|
- var selectedUsers = users
|
|
|
- .SelectMany(u => u.Languages,
|
|
|
- (u, l) => new { Student = u, Lang = l })
|
|
|
- .Where(u => u.Lang == "китайский" && u.Student.Age < 25)
|
|
|
- .Select(u => u.Student);
|
|
|
-
|
|
|
- Console.WriteLine("Студенты, изучающие китайский младше 25 лет:");
|
|
|
- foreach (var user in selectedUsers)
|
|
|
- {
|
|
|
- Console.WriteLine($"{user.Name}, возраст: {user.Age}");
|
|
|
- }
|
|
|
- }
|
|
|
+ Console.WriteLine(t.Name);
|
|
|
}
|
|
|
```
|
|
|
```
|
|
|
-Студенты, изучающие китайский младше 25 лет:
|
|
|
-Арсен, возраст: 21
|
|
|
-Глория, возраст: 23
|
|
|
+ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
|
|
|
+Student
|
|
|
```
|
|
|
-## Проекция
|
|
|
-### нам нужен не весь объект, а только его свойство Name:
|
|
|
+## Исследование типов
|
|
|
+### Получение типа через typeof:
|
|
|
```
|
|
|
-using System;
|
|
|
-using System.Linq;
|
|
|
+Type myType = typeof(Student);
|
|
|
|
|
|
-class Student
|
|
|
-{
|
|
|
- public string Name { get; set; }
|
|
|
- public int Age { get; set; }
|
|
|
-}
|
|
|
+Console.WriteLine(myType.ToString());
|
|
|
+Console.ReadLine();
|
|
|
|
|
|
-class Program
|
|
|
-{
|
|
|
- static void Main()
|
|
|
- {
|
|
|
- List<Student> users = new List<Student>();
|
|
|
- users.Add(new Student { Name = "Мадина", Age = 20 });
|
|
|
- users.Add(new Student { Name = "Максим", Age = 23 });
|
|
|
- users.Add(new Student { Name = "Макар", Age = 21 });
|
|
|
-
|
|
|
- var names = users.Select(u => u.Name);
|
|
|
-
|
|
|
- foreach (string n in names)
|
|
|
- Console.WriteLine(n);
|
|
|
- }
|
|
|
-}
|
|
|
-```
|
|
|
-```
|
|
|
-Мадина
|
|
|
-Максим
|
|
|
-Макар
|
|
|
-```
|
|
|
-### можно создать объекты другого типа, в том числе анонимного:
|
|
|
-```
|
|
|
-using System;
|
|
|
-using System.Linq;
|
|
|
-
|
|
|
-class Student
|
|
|
+public class Student
|
|
|
{
|
|
|
public string Name { get; set; }
|
|
|
public int Age { get; set; }
|
|
|
-}
|
|
|
-
|
|
|
-class Program
|
|
|
-{
|
|
|
- static void Main()
|
|
|
+ public Student(string n, int a)
|
|
|
{
|
|
|
- List<Student> users = new List<Student>();
|
|
|
- users.Add(new Student { Name = "Мадина", Age = 20 });
|
|
|
- users.Add(new Student { Name = "Максим", Age = 23 });
|
|
|
- users.Add(new Student { Name = "Макар", Age = 21 });
|
|
|
-
|
|
|
- var items = users.Select(u => new {
|
|
|
- FirstName = u.Name,
|
|
|
- DateOfBirth = DateTime.Now.Year - u.Age
|
|
|
- });
|
|
|
-
|
|
|
- foreach (var n in items)
|
|
|
- Console.WriteLine($"{n.FirstName} - {n.DateOfBirth}");
|
|
|
+ Name = n;
|
|
|
+ Age = a;
|
|
|
}
|
|
|
-}
|
|
|
-```
|
|
|
-```
|
|
|
-Мадина - 2004
|
|
|
-Максим - 2001
|
|
|
-Макар - 2003
|
|
|
-```
|
|
|
-## Переменые в запросах и оператор let
|
|
|
-```
|
|
|
-using System;
|
|
|
-using System.Linq;
|
|
|
-
|
|
|
-class Student
|
|
|
-{
|
|
|
- public string Name { get; set; }
|
|
|
- public int Age { get; set; }
|
|
|
-}
|
|
|
-
|
|
|
-class Program
|
|
|
-{
|
|
|
- static void Main()
|
|
|
+ public void Display()
|
|
|
{
|
|
|
- List<Student> students = new List<Student>()
|
|
|
- {
|
|
|
- new Student
|
|
|
- {
|
|
|
- Name = "Алеша", Age = 19
|
|
|
- },
|
|
|
- new Student
|
|
|
- {
|
|
|
- Name = "Гоша", Age = 22
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- var people = from u in students
|
|
|
- let name = "Mr. " + u.Name
|
|
|
- select new
|
|
|
- {
|
|
|
- Name = name,
|
|
|
- Age = u.Age
|
|
|
- };
|
|
|
- foreach (var person in people)
|
|
|
- {
|
|
|
- Console.WriteLine($" {person.Name}");
|
|
|
- }
|
|
|
+ Console.WriteLine($"Имя: {Name} Возраст: {Age}");
|
|
|
}
|
|
|
-}
|
|
|
-```
|
|
|
-```
|
|
|
- Mr. Алеша
|
|
|
- Mr. Гоша
|
|
|
-```
|
|
|
-## Выборка из нескольких источников
|
|
|
-```
|
|
|
-using System;
|
|
|
-using System.Linq;
|
|
|
-
|
|
|
-class Lesson
|
|
|
-{
|
|
|
- public string Name { get; set; }
|
|
|
-}
|
|
|
-class Student
|
|
|
-{
|
|
|
- public string Name { get; set; }
|
|
|
- public int Age { get; set; }
|
|
|
-}
|
|
|
-
|
|
|
-class Program
|
|
|
-{
|
|
|
- static void Main()
|
|
|
+ public int registration (int today, int tomorrow)
|
|
|
{
|
|
|
-
|
|
|
- List<Student> users = new List<Student>()
|
|
|
- {
|
|
|
- new Student { Name = "Влад", Age = 19 },
|
|
|
- new Student { Name = "Влада", Age = 21 }
|
|
|
- };
|
|
|
- List<Lesson> lessons = new List<Lesson>()
|
|
|
- {
|
|
|
- new Lesson
|
|
|
- {
|
|
|
- Name = "math"
|
|
|
- },
|
|
|
- new Lesson
|
|
|
- {
|
|
|
- Name = "psychology"
|
|
|
- },
|
|
|
- };
|
|
|
-
|
|
|
- var people = from student in users
|
|
|
- from lesson in lessons
|
|
|
- select new { Name = student.Name, Lesson = lesson.Name };
|
|
|
-
|
|
|
- foreach (var p in people)
|
|
|
- Console.WriteLine($"{p.Name} - {p.Lesson}");
|
|
|
+ return today * tomorrow;
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
```
|
|
|
-Влад - math
|
|
|
-Влад - psychology
|
|
|
-Влада - math
|
|
|
-Влада - psychology
|
|
|
+Гоша, 18 лет
|
|
|
```
|
|
|
-## Сортировка
|
|
|
+## Позднее связывание
|
|
|
```
|
|
|
using System;
|
|
|
-using System.Linq;
|
|
|
|
|
|
-class Student
|
|
|
-{
|
|
|
- public string Name { get; set; }
|
|
|
- public int Age { get; set; }
|
|
|
-}
|
|
|
class Program
|
|
|
{
|
|
|
- static void Main()
|
|
|
+ static void Main(string[] args)
|
|
|
{
|
|
|
-
|
|
|
- List<Student> users = new List<Student>()
|
|
|
- {
|
|
|
- new Student { Name = "Арсен", Age = 18 },
|
|
|
- new Student { Name = "Семен", Age = 29 },
|
|
|
- new Student { Name = "Полина", Age = 27 },
|
|
|
- new Student { Name = "Кирилл", Age = 21 }
|
|
|
- };
|
|
|
-
|
|
|
- var sortedStudent = users.OrderByDescending(u => u.Name);
|
|
|
-
|
|
|
- foreach (Student u in sortedStudent)
|
|
|
- Console.WriteLine(u.Name);
|
|
|
+ University university = new University();
|
|
|
+ university.InvestInEducation(5, 1000000, 4); // Инвестиция в образование студентов на 4 года с 5% дохода в год
|
|
|
+ Console.WriteLine($"Итоговый капитал университета: {university.Capital}");
|
|
|
+ Console.ReadLine();
|
|
|
}
|
|
|
}
|
|
|
-```
|
|
|
-```
|
|
|
-Семен
|
|
|
-Полина
|
|
|
-Кирилл
|
|
|
-Арсен
|
|
|
-```
|
|
|
-## Множественные критерии сортировки
|
|
|
-```
|
|
|
-class Student
|
|
|
-{
|
|
|
- public string Name { get; set; }
|
|
|
- public int Age { get; set; }
|
|
|
-}
|
|
|
|
|
|
-class Program
|
|
|
+class University
|
|
|
{
|
|
|
- static void Main()
|
|
|
+ public double Capital { get; set; }
|
|
|
+
|
|
|
+ public void InvestInEducation(int percent, double initialCapital, int years)
|
|
|
{
|
|
|
- List<Student> users = new List<Student>()
|
|
|
+ Capital = initialCapital;
|
|
|
+ for (int i = 0; i < years; i++)
|
|
|
{
|
|
|
- new Student { Name = "Амир", Age = 23 },
|
|
|
- new Student { Name = "Егор", Age = 30 },
|
|
|
- new Student { Name = "Настя", Age = 21 },
|
|
|
- new Student { Name = "Анжела", Age = 19 }
|
|
|
- };
|
|
|
-
|
|
|
- var result = users
|
|
|
- .OrderBy(u => u.Name)
|
|
|
- .ThenBy(u => u.Age);
|
|
|
-
|
|
|
- foreach (Student u in result)
|
|
|
- Console.WriteLine($"{u.Name} - {u.Age}");
|
|
|
+ Capital += Capital / 100 * percent;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
```
|
|
|
-Амир - 23
|
|
|
-Анжела - 19
|
|
|
-Егор - 30
|
|
|
-Настя - 21
|
|
|
-```
|
|
|
+Итоговый капитал университета: 1215506,25
|
|
|
+```
|