当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > LINQ学习笔记:创建方法

ASP.NET
FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用方法
.NET 常用功能和代码小结
在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意外退出
asp.net IList查询数据后格式化数据再绑定控件
asp.net sql存储过程
asp.net 简单实现禁用或启用页面中的某一类型的控件
asp.net(c#)获取内容第一张图片地址的函数
The remote procedure call failed and did not execute的解决办法
ASP.NET 在线文件管理
asp.net 读取并修改config文件实现代码
ASP.NET Cookie 操作实现
asp.net Silverlight中的模式窗体
Silverlight中动态获取Web Service地址
asp.net Silverlight应用程序中获取载体aspx页面参数
asp.net 水晶报表隔行换色实现方法
asp.net 获取Gridview隐藏列的值
手动把asp.net的类生成dll文件的方法
asp.net 使用ObjectDataSource控件在ASP.NET中实现Ajax真分页
动态指定任意类型的ObjectDataSource对象的查询参数
asp.net Md5的用法小结

ASP.NET 中的 LINQ学习笔记:创建方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 53 ::
收藏到网摘: n/a

创建方法

方法 描述
Empty 创建一个空序列
Repeat 创建一个包含重复元素的序列
Range 创建一个包含整数的序列

Empty, Repeat和Range都是静态方法, 用于加工简单的本地序列

Empty

Empty制造一个空序列并且只需要一个类型参数:

 1: foreach (string s in Enumerable.Empty<string>())
 2: Console.Write(s); // <nothing>

与??操作符联合, Empty可以完成和DefaultIfEmpty相反的工作. 例如, 假设我们有一个锯齿形状的整型数组, 并且我们希望能够将所有的整数放入到一个扁平的列表当中. 以下的SelectMany将会失败因为输入序列包含了null的内部数组:

 1: int[][] numbers =
 2: {
 3: new int[] { 1, 2, 3 },
 4: new int[] { 4, 5, 6 },
 5: null // null会导致查询失败.
 6: };
 7:
 8: IEnumerable<int> flat =
 9: numbers.SelectMany (innerArray =>innerArray);

Empty联合??操作符可以修正此问题:

 1: IEnumerable<int> flat = numbers
 2: .SelectMany (innerArray =>
 3: innerArray ?? Enumerable.Empty <int>() );
 4:
 5: foreach (int i in flat)
 6: Console.Write (i + " "); // 1 2 3 4 5 6

Range和Repeat

Range和Repeat只能与整数一起工作. Range接受一个起始索引和取值总数:

 1: foreach (int i in Enumerable.Range (5,5))
 2: Console.Write (i + " "); // 5 6 7 8 9

Repeat接受一个接受重复的数字以及要重复的次数的参数:

 1: foreach (int i in Enumerable.Repeat (5,3))
 2: Console.Write (i + " "); // 5 5 5

待续!