امکانات LINQ در NET 6.
✅ ۱- امکان مشخص کردن مقدار پیشفرض در OrDefault*
var item1 = list1.FirstOrDefault(i => i == 4, -1);
// -1
var item2 = list2.SingleOrDefault(i => i == "Item2", "Not found");
// Not found
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖
✅۲- متدهای جدید با مدل By*:
- MinBy
- MaxBy
- DistinctBy
- ExceptBy
- IntersectBy
- UnionBy
List<Product> products = new()
{
new() { Name = "Product1", Price = 100 },
new() { Name = "Product2", Price = 5 },
new() { Name = "Product3", Price = 50 },
};
Product theCheapestProduct = products.MinBy(x => x.Price);
Product theMostExpensiveProduct = products.MaxBy(x => x.Price);
Console.WriteLine(theCheapestProduct);
// Output: Product { Name = Product2, Price = 5 }
Console.WriteLine(theMostExpensiveProduct);
// Output: Product { Name = Product1, Price = 100 }
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖
✅۳- متد کاربردی Chunk :
IEnumerable<int> numbers = Enumerable.Range(1, 505);
IEnumerable<int[]> chunks = numbers.Chunk(100);
foreach (int[] chunk in chunks)
{
Console.WriteLine($"{chunk.First()}...{chunk.Last()}");
}
// Output:
// 1...100
// 101...200
// 201...300
// 301...400
// 401...500
// 501...505
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖
✅۴- تابع Zip
int[] numbers = { 1, 2, 3, 4, };
string[] months = { "Jan", "Feb", "Mar" };
string[] seasons = { "Winter", "Winter", "Spring" };
var test = numbers.Zip(months).Zip(seasons);
foreach ((int, string, string) zipped in numbers.Zip(months, seasons))
{
Console.WriteLine($"{zipped.Item1} {zipped.Item2} {zipped.Item3}");
}
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖
✅۵- پشتیبانی از Index در تابع ElementAt :
IEnumerable<int> numbers = new int[] { 1, 2, 3, 4, 5 };
int last = numbers.ElementAt(^0);
Console.WriteLine(last); // 5
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖
✅۶- پشتیبانی از Range در تابع Take :
var taken1 = numbers.Take(2..4);
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖
✅۷- جلوگیری از شمارش تایپهای غیر Enumerable:
numbers.TryGetNonEnumeratedCount(out int count)
➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖➖
https://raygun.com/blog/linq-net-6-improvements/
⁉️ برای بحث و تبادل نظر فنی د