1. 기본적인 LINQ 쿼리 문법
using System.Linq;
var numbers = new int[] { 1, 2, 3, 4, 5 };
var evenNumbers = from num in numbers
where num % 2 == 0
select num;
2. 메소드 체인을 사용한 LINQ
var oddNumbers = numbers.Where(num => num % 2 != 0).ToList();
3. 정렬 (Ordering)
var sortedNumbers = from num in numbers
orderby num descending
select num;
var sortedNumbersMethod = numbers.OrderByDescending(num => num).ToList();
4. 집계 함수 (Aggregation)
int sum = numbers.Sum();
int max = numbers.Max();
int min = numbers.Min();
5. 그룹화 (Grouping)
var groupedByParity = from num in numbers
group num by num % 2 into numGroup
select new { Parity = numGroup.Key, Numbers = numGroup };
var groupedByParityMethod = numbers.GroupBy(num => num % 2)
.Select(g => new { Parity = g.Key, Numbers = g })
.ToList();
6. 조인 (Join)
var products = new[] { new { Id = 1, Name = "Apple" }, new { Id = 2, Name = "Banana" } };
var orders = new[] { new { ProductId = 1, Quantity = 5 }, new { ProductId = 2, Quantity = 3 } };
var productOrders = from p in products
join o in orders on p.Id equals o.ProductId
select new { p.Name, o.Quantity };
7. 선택 (Projection)
var productNames = products.Select(p => p.Name).ToList();