C#高级功能(二)LINQ 和Enumerable类
介绍LINQ之前先介绍一下枚举器
Iterator:枚举器
如果你正在创建一个表现和行为都类似于集合的类,允许类的用户使用foreach语句对集合中的成员进行枚举将会是很方便的。
我们将以创建一个简单化的List Box作为开始,它将包含一个8字符串的数组和一个整型,这个整型用于记录数组中已经添加了多少字符串。
构造函数将对数组进行初始化并使用传递进来的参数填充它。
/// <summary>
/// Iterator:枚举器
/// 测试枚举器,继承IEnumerable,实现IEnumerator<T> GetEnumerator()
/// </summary>
/// <typeparam name="T"></typeparam>
public class ListBox<T> : IEnumerable<T>
{
public List<T> Strings = new List<T>(); public ListBox(params T[] initialStrings)
{
foreach (T s in initialStrings)
{
Strings.Add(s);
}
} IEnumerator<T> IEnumerable<T>.GetEnumerator()//这个方法和下面的方法都必须实现
{
foreach (T s in Strings)
{
yield return s;
}
}
//GetEnumerator方法使用了新的 yield 语句。yield语句返回一个表达式。yield语句仅在迭代块中出现,
//并且返回foreach语句所期望的值。那也就是,对GetEnumerator的每次调用都//将会产生集合中的下一个字符串;所有的状态管理已经都为你做好了! IEnumerator IEnumerable.GetEnumerator()//这个方法和上面的方法都必须实现
{
throw new NotImplementedException();
}
}
语言集成查询 (LINQ) 是一组技术的名称,这些技术建立在将查询功能直接集成到 C# 语言(以及 Visual Basic
和可能的任何其他 .NET 语言)的基础上。 借助于 LINQ,查询现在已是高级语言构造,就如同类、方法、事件等等。
对于编写查询的开发人员来说,LINQ 最明显的“语言集成”部分是查询表达式。
查询表达式是使用 C# 3.0 中引入的声明性查询语法编写的。
通过使用查询语法,您甚至可以使用最少的代码对数据源执行复杂的筛选、排序和分组操作。
您使用相同的基本查询表达式模式来查询和转换 SQL 数据库、ADO.NET 数据集、XML 文档和流以及 .NET 集合中的数据。
LINQ的基本格式如下所示:
var <变量> = from <项目> in <数据源> where <表达式> orderby <表达式>
group分组子句
语句格式:var str = from p in PersonList group p by p.age
group子句将数据源中的数据进行分组,在遍历数据元素时,并不像前面的章节那样直接对元素进行遍历,因为group子句返回的是元素类型为IGrouping<TKey,TElement>的对象序列,必须在循环中嵌套一个对象的循环才能够查询相应的数据元素。
在使用group子句时,LINQ查询子句的末尾并没有select子句,因为group子句会返回一个对象序列,通过循环遍历才能够在对象序列中寻找到相应的对象的元素,如果使用group子句进行分组操作,可以不使用select子句。 orderby排序子句
语句格式:var str = from p in PersonList orderby p.age select p;
orderby子句中使用descending关键字进行倒序排列
示例代码如下:
var str = from p in PersonList orderby p.age descending select p;
orderby子句同样能够进行多个条件排序,只需要将这些条件用“,”号分割即可
示例代码如下:
var str = from p in PersonList orderby p.age descending,p.name select p; join连接子句
在LINQ中同样也可以使用join子句对有关系的数据源或数据对象进行查询,但首先这两个数据源必须要有一定的联系
var str = from p in PersonList join car in CarList on p.cid equals car.cid select p;
下面的这个是LInq中常用的类
Enumerable 类 (System.Linq)
public class LinqClass
{
public static void Test1()
{
int[] scores = { , , , , , , , , , };
IEnumerable<int> scoreQuery =
from score in scores //必须
where score > //可选条件
orderby score descending //可选条件
select score; //必须
foreach (int score in scoreQuery)
{
Console.WriteLine(score);
} int hightestScore = scores.Max(); //group 分组
var queryGroups =
from score in scores
group score by score; //into 存储查询的内容
//// percentileQuery is an IEnumerable<IGrouping<int, Country>>
var percentileQuery =
from score in scores
group score by score into scorequery
where scorequery.Key >
select scorequery; //在 from 开始子句以及 select 或 group 结束子句之间,
//所有其他子句(where、join、orderby、from、let)都是可选的。
//任何可选子句都可以在查询正文中使用零次或多次。 //let 子句
//使用 let 子句可以将表达式(如方法调用)的结果存储到新的范围变量中。
string[] names = { "a n", "b c", "c n", "d m" };
IEnumerable<string> queryFirstNames =
from name in names
let firstName = name.Split(new char[] { ' ' })[]
select firstName; //对一个序列应用累加器函数。
//Aggregate<TSource>(IEnumerable<TSource>, Func<TSource, TSource, TSource>)
string sentence = "the quick brown fox jumps over the lazy dog";
string[] words = sentence.Split(' ');
string reversed = words.Aggregate((wording, next) => wording + " " + next);
Console.WriteLine(reversed); //确定是否对序列中的所有元素都满足条件。
//All<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>) //确定序列是否包含任何元素。
//Any<TSource>(IEnumerable<TSource>)
List<int> numbers = new List<int> { , };
bool hasElements = numbers.Any();
Console.WriteLine("The list {0} empty.",
hasElements ? "is not" : "is"); // 返回输入类型化为 IEnumerable<T>。
// AsEnumerable<TSource>(IEnumerable<TSource>) //将强制转换的元素 IEnumerable 为指定的类型。符合规则热任意类型,与上面的区别。
//Cast<TResult>(IEnumerable) //计算序列的平均值 Double 值。
//Average(IEnumerable<Double>) //连接两个序列。
//Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) //确定序列是否包含指定的元素使用的默认相等比较器。
//Contains<TSource>(IEnumerable<TSource>, TSource) //返回序列中的元素数。
//Count<TSource>(IEnumerable<TSource>) //返回单一实例集合中指定的序列或类型参数的默认值的元素,如果序列为空。
//DefaultIfEmpty<TSource>(IEnumerable<TSource>) //通过使用的默认相等比较器对值进行比较从序列返回非重复元素。
//Distinct<TSource>(IEnumerable<TSource>) //返回序列中的指定索引处的元素。
//ElementAt<TSource>(IEnumerable<TSource>, Int32) //通过使用默认的相等比较器对值进行比较,生成两个序列的差集。
//Except<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
double[] numbers1 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 };
double[] numbers2 = { 2.2 };
IEnumerable<double> onlyInFirstSet = numbers1.Except(numbers2);
foreach (double number in onlyInFirstSet)
Console.WriteLine(number); //返回一个序列的第一个元素。
//First<TSource>(IEnumerable<TSource>) //返回序列中满足指定条件的第一个元素。
//FirstOrDefault<TSource>(IEnumerable<TSource>) //根据指定的键选择器函数对序列的元素进行分组。
//GroupBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>) // 通过使用默认的相等比较器对值进行比较,生成两个序列的交集。
//Intersect<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)
int[] id1 = { , , , , , };
int[] id2 = { , , , , , , };
IEnumerable<int> both = id1.Intersect(id2);
foreach (int id in both)
Console.WriteLine(id); //通过使用默认的相等比较器生成的两个序列的并集。
//Union<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) //返回一个序列的最后一个元素。
//Last<TSource>(IEnumerable<TSource>) // 从序列的开头返回指定的数量的连续元素。
// Take<TSource>(IEnumerable<TSource>, Int32) // 返回序列中的最大值 Decimal 值。
//Max(IEnumerable<Decimal>) //返回序列的唯一元素;如果该序列并非恰好包含一个元素,则会引发异常。
//Single<TSource>(IEnumerable<TSource>) //ToDictionary<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>) //Where<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>) // 一个序列的每个元素投影 IEnumerable<T> 并将合并为一个序列将结果序列。
//SelectMany<TSource, TResult>(IEnumerable<TSource>, Func<TSource, IEnumerable<TResult>>)
}
}
其他的基础功能
5. C#中泛型的解释(object,list,var,dynamic的区别)
6. C#中委托
7. C#和.NET版本对比
C#高级功能(二)LINQ 和Enumerable类的更多相关文章
- python3 高级编程(二) 动态给类添加方法功能
class Student(object): pass 给实例绑定一个属性: >>> s = Student() >>> s.name = 'Michael' # ...
- C#高级功能(四)扩展方法和索引
扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用.扩展方法被定义为静态方法,但 ...
- C#高级功能(三)Action、Func,Tuple
Action和Func泛型委托实际上就是一个.NET Framework预定义的委托,3.5引入的特性.基本涵盖了所有常用的委托,所以一般不用用户重新声明. Action系列泛型委托,是没有返回参数的 ...
- C#高级功能(一)Lambda 表达式
Lambda 表达式是一种可用于创建委托或表达式目录树类型的匿名函数. 通过使用 lambda 表达式,可以写入可作为参数传递或作为函数调用值返回的本地函数. Lambda 表达式对于编写 LINQ ...
- ASP.NET MVC深入浅出(被替换) 第一节: 结合EF的本地缓存属性来介绍【EF增删改操作】的几种形式 第三节: EF调用普通SQL语句的两类封装(ExecuteSqlCommand和SqlQuery ) 第四节: EF调用存储过程的通用写法和DBFirst模式子类调用的特有写法 第六节: EF高级属性(二) 之延迟加载、立即加载、显示加载(含导航属性) 第十节: EF的三种追踪
ASP.NET MVC深入浅出(被替换) 一. 谈情怀-ASP.NET体系 从事.Net开发以来,最先接触的Web开发框架是Asp.Net WebForm,该框架高度封装,为了隐藏Http的无状态 ...
- 整理一下 System.Linq.Enumerable 类中的那些比较少用的方法
Linq 虽然用得多,但是里面有一些方法比较少用,因此整理一下.Enumerable 类的所有方法可以在 MSDN 上查阅到:https://msdn.microsoft.com/zh-cn/libr ...
- SLAM+语音机器人DIY系列:(二)ROS入门——10.在实际机器人上运行ROS高级功能预览
摘要 ROS机器人操作系统在机器人应用领域很流行,依托代码开源和模块间协作等特性,给机器人开发者带来了很大的方便.我们的机器人“miiboo”中的大部分程序也采用ROS进行开发,所以本文就重点对ROS ...
- iOS开发——UI篇Swift篇&玩转UItableView(二)高级功能
UItableView高级功能 class UITableViewControllerAF: UIViewController, UITableViewDataSource, UITableViewD ...
- 为ASP.NET MVC应用程序使用高级功能
为ASP.NET MVC应用程序使用高级功能 这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译, ...
随机推荐
- hbase的regionserver宕机
错误日志: regionserver.HRegionServer: Failed deleting my ephemeral node zookeeper.RecoverableZooKeeper: ...
- (转)adb shell am 的用法
原文地址:http://blog.csdn.net/fulinwsuafcie/article/details/8092459 adb shell am 的功能 adb shell am 使用此命 ...
- MapReduce从输入文件到Mapper处理之间的过程
1.MapReduce代码入口 FileInputFormat.setInputPaths(job, new Path(input)); //设置MapReduce输入格式 job.waitForCo ...
- jdk集合常用方法分析之ArrayList&LinkedList&以及两者的对比分析
集合使用注意事项: 1.集合当中只能放置对象的引用,无法放置原生数据类型,我们需要使用原生数据类型的包装类才能加入到集合当中去(JDK5之后会进行自动的装箱和拆箱操作,表面上看集合中是可以直接放置原生 ...
- 欧拉路径Hrbust1351
http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1351 这道题先利用并查集的知识点, ...
- android下拉菜单 spinner 学习
首先看一下继承关系: public class Spinner extends AbsSpinner implements DialogInterface.OnClickListener Class ...
- 单源最短路_SPFA_C++
当我们需要求一个点到其它所有点的最短路时,我们可以采用SPFA算法 代码特别好写,而且可以有环,但是不能有负权环,时间复杂度是O(α(n)n),n为边数,α(n)为n的反阿克曼函数,一般小于等于4 模 ...
- maven + appium + testng + java之pom.xml
参考来源:<https://search.maven.org/remotecontent?filepath=io/appium/java-client/3.3.0/java-client-3.3 ...
- 个人收集(转载)CSS中 display:none和visibility:hidden的区别
visibility和display两个属性都有隐藏元素的功能,display:none和visibility:hidden的区别,简单的总结一句话就是:visibility:hidden隐藏,但在浏 ...
- 慕课网-安卓工程师初养成-1-6 MyEclipse的使用简介
来源 http://www.imooc.com/video/1414 http://www.my-eclipse.cn/ MyEclipse 2014 官方版下载地址 声明:MyEclipse 20 ...