目錄
概述
二十、SelectMany
二十一、Aggregate
二十二、DistinctBy
二十三、Reverse
二十四、SequenceEqual
二十五、Zip
二十六、SkipWhile?
二十七、TakeWhile
C# Linq 文檔(一)
1.Where,2.Select,3.GroupBy,4.First / FirstOrDefault,5.Last / LastOrDefault
C# Linq 文檔(二)
1.OrderBy ,2.OrderByDescending,3.Skip,4.Take,5.Any,6.All
C# Linq 文檔(三)
1.Sum / Min / Max / Average,2.Distinct,3.Concat,4.Join,5.ToList ,6.ToArray,7.ToDictionary
C# Linq 文檔(四)
1.SelectMany,2.Aggregate,3.DistinctBy,4.Reverse,5.SequenceEqual,6.Zip,7.SkipWhile ,8.TakeWhile
C# Linq 文檔(一)_熊思宇的博客-CSDN博客
C# Linq 文檔(二)_熊思宇的博客-CSDN博客
C# Linq 文檔(三)_熊思宇的博客-CSDN博客
概述
語言集成查詢 (LINQ) 是一系列直接將查詢功能集成到 C# 語言的技術統(tǒng)稱。 數(shù)據查詢歷來都表示為簡單的字符串,沒有編譯時類型檢查或 IntelliSense 支持。 此外,需要針對每種類型的數(shù)據源了解不同的查詢語言:SQL 數(shù)據庫、XML 文檔、各種 Web 服務等。 借助 LINQ,查詢成為了最高級的語言構造,就像類、方法和事件一樣。
對于編寫查詢的開發(fā)者來說,LINQ 最明顯的“語言集成”部分就是查詢表達式。 查詢表達式采用聲明性查詢語法編寫而成。 使用查詢語法,可以用最少的代碼對數(shù)據源執(zhí)行篩選、排序和分組操作。 可使用相同的基本查詢表達式模式來查詢和轉換 SQL 數(shù)據庫、ADO .NET 數(shù)據集、XML 文檔和流以及 .NET 集合中的數(shù)據。
二十、SelectMany
SelectMany 用于將嵌套的集合展開成一個扁平的序列
這個解釋有點難以理解,用案例來解釋最好不過了
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqTest
{
internal class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>
{
new Student { Id = 1, Name = "John", Courses = new List<string> { "Math", "English" } },
new Student { Id = 2, Name = "Jane", Courses = new List<string> { "Science", "History" } },
new Student { Id = 3, Name = "Mike", Courses = new List<string> { "Art", "Music" } }
};
var allCourses = students.SelectMany(s => s.Courses);
foreach (var course in allCourses)
{
Console.WriteLine(course);
}
Console.ReadKey();
}
}
class Student
{
public int Id { get; set; }
public string Name { get; set; }
public List<string> Courses { get; set; }
}
}
運行:
二十一、Aggregate
使用指定的累加器函數(shù)對序列中的元素進行累積計算。
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqTest
{
internal class Program
{
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4, 5 };
int sum = numbers.Aggregate((acc, num) => acc + num);
Console.WriteLine("結果:{0}", sum);
Console.ReadKey();
}
}
}
運行:
在?Aggregate((acc, num) => acc + num)?這句可以看到,有兩個參數(shù),是這樣,它是先取兩個參數(shù),1 + 2 = 3,第二次再拿 3 + 3 = 6,如此類推,最終的結果等于 15
二十二、DistinctBy
DistinctBy 是一個自定義的擴展方法,它用于按照指定的屬性或條件對序列中的元素進行去重操作。
去重操作,看的有點蒙,其實就是去除重復的元素,這個方法用法也是花樣很多,可以搜到很多種玩法,只是我基本不用的,哈哈。
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqTest
{
internal class Program
{
static void Main(string[] args)
{
List<Person> people = new List<Person>
{
new Person { Id = 1, Name = "John" },
new Person { Id = 2, Name = "Jane" },
new Person { Id = 3, Name = "John" }
};
var distinctPeople = people.DistinctBy(p => p.Name);
foreach (var person in distinctPeople)
{
Console.WriteLine(person.Name);
}
Console.ReadKey();
}
}
class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
public static class EnumerableExtensions
{
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector)
{
HashSet<TKey> seenKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (seenKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
}
}
運行:
二十三、Reverse
將序列中的元素按照相反的順序返回。
這個非常簡單,直接上案例
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqTest
{
internal class Program
{
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4, 5 };
var reversedNumbers = numbers.Reverse();
foreach (var number in reversedNumbers)
{
Console.WriteLine(number);
}
Console.ReadKey();
}
}
}
運行:
二十四、SequenceEqual
SequenceEqual 用于比較兩個序列是否相等。
這個方法,兩個數(shù)組必須一模一樣才會返回 true,而且類型也必須一樣
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqTest
{
internal class Program
{
static void Main(string[] args)
{
int[] numbers1 = { 1, 2, 3, 4, 5 };
int[] numbers2 = { 1, 2, 3, 4, 5 };
bool areEqual = numbers1.SequenceEqual(numbers2);
Console.WriteLine(areEqual);
Console.ReadKey();
}
}
}
運行:
二十五、Zip
zip 用于將兩個序列按照索引位置進行配對。
下面這個案例中,把兩個數(shù)組一對一進行相加,并根據原來的下標,重新生成了一個新的數(shù)組
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqTest
{
internal class Program
{
static void Main(string[] args)
{
int[] numbers1 = { 1, 2, 3 };
int[] numbers2 = { 10, 20, 30 };
var zippedNumbers = numbers1.Zip(numbers2, (n1, n2) => n1 + n2);
foreach (var number in zippedNumbers)
{
Console.WriteLine(number);
}
Console.ReadKey();
}
}
}
運行:
二十六、SkipWhile?
SkipWhile 用于跳過序列中滿足指定條件的元素,直到遇到第一個不滿足條件的元素。
注意這里,是不滿足條件,下面的案例中,n 指的就是數(shù)組中的每個元素,在遍歷到 n 不滿足? n < 3 這個條件時,才開始返回集合。
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqTest
{
internal class Program
{
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4, 5 };
var skippedNumbers = numbers.SkipWhile(n => n < 3);
foreach (var number in skippedNumbers)
{
Console.WriteLine(number);
}
Console.ReadKey();
}
}
}
運行:
二十七、TakeWhile
TakeWhile 用于獲取序列中滿足指定條件的連續(xù)元素,直到遇到第一個不滿足條件的元素。
TakeWhile 和?SkipWhile 剛好相反,用起來也是比較簡單的。
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqTest
{
internal class Program
{
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4, 5 };
var takenNumbers = numbers.TakeWhile(n => n < 4);
foreach (var number in takenNumbers)
{
Console.WriteLine(number);
}
Console.ReadKey();
}
}
}
運行:
文章來源:http://www.zghlxwxcb.cn/news/detail-564383.html
end文章來源地址http://www.zghlxwxcb.cn/news/detail-564383.html
到了這里,關于C# Linq 詳解四的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!