C#中的Linq 學習 投影、篩選和排序

Select

select 在一個集合序列按給定的條件進行投影,select 可以返回組合的篩選結果,返回匿名類型,對返回結果進行操作,返回組合的子查詢結果等等。
select 的方法定義原形爲:
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)

該擴展方法是在Enumerable類型中定義的。

// 數據源的類型的屬性
var result = from student in DataSource.Students
             where student.Name.Length > 3
             select student.Name;

// 數據源類型篩選後的結果
var result = from student in DataSource.Students
             where student.Name.Length > 3
             select student;

// 新類型
var result = from student in DataSource.Students
             where student.Name.Length > 3
             select new Student { Name = student.Name, StudentID = student.StudentID };

// 匿名類型
var result = from student in DataSource.Students
             where student.Name.Length > 3
             select new { Name = student.Name, StudentID = student.StudentID };

// 對返回結果進行操作
var result = from student in DataSource.Students
             where student.Name.Length > 3
             select student.ToString();

由Select方法原型可看出,返回結果爲:IEnumerable<T>類型。

SelectMany

SelectMany定義原型爲:
public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector)

通過原型可以看出,篩選結果的每一個元素類型都應該實現IEnumerable<T>接口。
string實現了IEnumerable<T>接口,可以構造這樣的場景:查詢組成學生姓名的所有字符序列。
var result = DataSource.Students.SelectMany(str => str.Name);

等價的Select 子句爲:
var result = from student in DataSource.Students
             from ch in student.Name
             select ch;

可以認爲SelectMany是將序列的每個元素投影到 IEnumerable<(Of <(T>)>) 並將結果序列合併爲一個序列。

Distinct

原型爲:
public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source)
去掉投影結果集的重複元素。該運算只能以方法調用的方式進行操作。
上面同樣的場景:查詢組成學生姓名的所有字符的不重複序列。
var result = DataSource.Students.SelectMany(str => str.Name).Distinct();

First、Last、Skip、Take、Single

First 選擇集和的第一個元素。
var result = DataSource.Students.Select(student => student).First(); // Student ID:1,Student Name:Andy
Last 選擇集和的最後一個元素。
var result = DataSource.Students.Select(student => student).Last(); // Student ID:4,Student Name:Dark
Skip 跳過N個元素。
var result = DataSource.Students.Select(student => student).Skip(2).Count(); // 2
Take 選擇集合的前N個元素。
var result = DataSource.Students.Select(student => student).Skip(2).Take(1).Count(); // 1
Single 返回序列的唯一元素;如果該序列並非恰好包含一個元素,則會引發異常。
var result = DataSource.Students.Select(student => student).Single(); // 異常:Sequence contains more than one element

OrderBy[Descending]

對集合進行排序。
public static IOrderedEnumerable<TSource> OrderBy[Descending]<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector[, IComparer<TKey> comparer])

TKey必須已經實現了IComparer<T>接口。

var result = from student in DataSource.Students
             orderby student.Name descending
             select student.Name;
// 等價於:
var result = DataSource.Students.OrderByDescending(student => student.Name).Select(student => student.Name);
// 結果:
// Dark
// Cindy
// Bill
// Andy

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章