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

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