Last active
March 28, 2024 05:30
-
-
Save normanlmfung/58be55a75db1cb4faf599c348445ca55 to your computer and use it in GitHub Desktop.
csharp_linq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
public class Book | |
{ | |
public string Region { get; set; } | |
public double AUM { get; set; } | |
} | |
public class Student | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
} | |
public class Course | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public int TeacherId { get; set; } | |
} | |
public class Teacher | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
} | |
public class Grade | |
{ | |
public int StudentId { get; set; } | |
public int CourseId { get; set; } | |
public int GradeValue { get; set; } | |
} | |
class Program { | |
static void Main(string[] args) { | |
IList<int> nums = new List<int> { 1, 3, 5, 7, 9, 11, 13, 15, 17 }; | |
var smallNums = from num in nums | |
where num < 10 | |
select num; | |
nums.Add(2); | |
int countSmallNums = smallNums.Count(); // Answer is 6, not 5. LINQ Lazy Evaluates! | |
// Filtering | |
var numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; | |
var evenNumbers = numbers.Where(n => n % 2 == 0); | |
foreach (var number in evenNumbers) | |
{ | |
Console.WriteLine(number); // Output: 2, 4, 6, 8, 10 | |
} | |
// Projection | |
var names = new List<string> { "John", "Jane", "Doe", "Alice" }; | |
var nameLengths = names.Select(name => name.Length); | |
foreach (var length in nameLengths) | |
{ | |
Console.WriteLine(length); // Output: 4, 4, 3, 5 | |
} | |
// Sorting | |
var unsortedNumbers = new List<int> { 5, 3, 8, 1, 2, 7, 4, 6 }; | |
var sortedNumbers = unsortedNumbers.OrderBy(n => n); | |
foreach (var number in sortedNumbers) | |
{ | |
Console.WriteLine(number); // Output: 1, 2, 3, 4, 5, 6, 7, 8 | |
} | |
// Aggregation | |
var numbers2 = new List<int> { 1, 2, 3, 4, 5 }; | |
var sum = numbers2.Sum(); | |
Console.WriteLine(sum); // Output: 15 | |
var books = new List<Book> | |
{ | |
new Book { Region = "APAC", AUM = 1000000 }, | |
new Book { Region = "EMEA", AUM = 1500000 }, | |
new Book { Region = "AMER", AUM = 2000000 }, | |
new Book { Region = "APAC", AUM = 1200000 }, | |
new Book { Region = "EMEA", AUM = 1800000 }, | |
new Book { Region = "AMER", AUM = 2200000 } | |
}; | |
// Anonymous Type | |
var groupedAUM = books.GroupBy(book => book.Region) | |
.Select(group => new | |
{ | |
Region = group.Key, | |
TotalAUM = group.Sum(book => book.AUM) | |
}); | |
foreach (var item in groupedAUM) | |
{ | |
Console.WriteLine($"Region: {item.Region}, Total AUM: {item.TotalAUM}"); | |
} | |
// Joining | |
var students = new List<Student> | |
{ | |
new Student { Id = 1, Name = "John" }, | |
new Student { Id = 2, Name = "Jane" }, | |
new Student { Id = 3, Name = "Doe" } | |
}; | |
var courses = new List<Course> | |
{ | |
new Course { Id = 1, Name = "Math", TeacherId = 1 }, | |
new Course { Id = 2, Name = "Physics", TeacherId = 2 } | |
}; | |
var teachers = new List<Teacher> | |
{ | |
new Teacher { Id = 1, Name = "Mr. Smith" }, | |
new Teacher { Id = 2, Name = "Mrs. Johnson" } | |
}; | |
var grades = new List<Grade> | |
{ | |
new Grade { StudentId = 1, CourseId = 1, GradeValue = 85 }, | |
new Grade { StudentId = 2, CourseId = 1, GradeValue = 90 }, | |
new Grade { StudentId = 3, CourseId = 1, GradeValue = 80 }, | |
new Grade { StudentId = 1, CourseId = 2, GradeValue = 88 }, | |
new Grade { StudentId = 2, CourseId = 2, GradeValue = 92 }, | |
new Grade { StudentId = 3, CourseId = 2, GradeValue = 85 } | |
}; | |
var result = from student in students | |
join grade in grades.Where(g => g.GradeValue>=80) | |
on student.Id equals grade.StudentId | |
join course in courses | |
on new { Name = "Math", CourseId = grade.CourseId } equals new { course.Name, CourseId = course.Id } | |
join teacher in teachers | |
on course.TeacherId equals teacher.Id | |
select new | |
{ | |
student.Name, | |
CourseName = course.Name, | |
grade.GradeValue, | |
TeacherName = teacher.Name | |
}; | |
foreach (var item in result) | |
{ | |
Console.WriteLine($"{item.Name} scored {item.GradeValue} in {item.CourseName} taught by {item.TeacherName}"); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment