Linq 入门 顺带 Func与Action 标准查询运算符概述 LINQ(语言集成查询) 导航 带 XML 声明的序列化 也就是保存 掌握了 Linq 后 , 就是 是.NET里面的内置委托 Func
Linq的优点:
可以使用相同的基本编码模式来查询和转换 XML 文档、SQL 数据库、ADO.NET 数据集、.NET 集合中的数据以及对其有 LINQ 提供程序可用的任何其他格式的数据。
地址 :https://msdn.microsoft.com/zh-cn/library/bb397906.aspx
深入学习 Linq 我只提个地址 , 不会在复制 , 这样便于日后快速查询
LINQ(语言集成查询) 导航
带 XML 声明的序列化 也就是保存
C#关键字 关于let的一个帮助理解的小示例: 我们一看就会知道,用了LET之后,层次感会更好一些,代码更易于阅读
int[] numbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; //传统下的子查询做法 var query = from num in numbers select num * (from n in numbers where n % 2 == 0 select n).Count(); //使用LET关键字的做法 //var query = from num in numbers // let evenNumbers = from n in numbers // where n % 2 == 0 // select n // select num * evenNumbers.Count(); foreach (var item in query) { Console.WriteLine(item); } Console.ReadKey();
-
获取数据源。
-
创建查询。
-
执行查询。
简单的示例:
int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 }; // 2. Query creation. // numQuery is an IEnumerable<int> var numQuery = from num in numbers where (num % 2) == 0 select num; // 3. Query execution. foreach (int num in numQuery) { Console.Write("{0,1} ", num); }
数据源
可查询类型。
例如,LINQ to XML 将 XML 文档加载到可查询的 XElement 类型中:
// Create a data source from an XML document. // using System.Xml.Linq; XElement contacts = XElement.Load(@"c:myContactList.xml");
Customers 表示数据库中的特定表,并且查询结果的类型 IQueryable<T> 派生自 IEnumerable<T>。
Northwnd db = new Northwnd(@"c: orthwnd.mdf"); // Query for customers in London. IQueryable<Customer> custQuery = from cust in db.Customers where cust.City == "London" select cust;
但基本规则非常简单:LINQ 数据源是支持泛型 IEnumerable<T> 接口或从该接口继承的接口的任意对象。
查询
为使编写查询的工作变得更加容易,C# 引入了新的查询语法。
有关在幕后是如何构建查询的更多信息,请参见标准查询运算符概述
执行查询
延迟执行
此概念称为“延迟执行”,下面的示例对此进行了演示:
// Query execution. foreach (int num in numQuery) { Console.Write("{0,1} ", num); }
num 保存了返回的序列中的每个值(一次保存一个值)。
在应用程序中,可以创建一个检索最新数据的查询,并可以按某一时间间隔反复执行该查询以便每次检索不同的结果。
强制立即执行
下面的查询返回源数组中偶数的计数:
var evenNumQuery = from num in numbers where (num % 2) == 0 select num; int evenNumCount = evenNumQuery.Count();
若要强制立即执行任意查询并缓存其结果,可以调用 ToList<TSource> 或 ToArray<TSource> 方法。
List<int> numQuery2 = (from num in numbers where (num % 2) == 0 select num).ToList(); // or like this: // numQuery3 is still an int[] var numQuery3 = (from num in numbers where (num % 2) == 0 select num).ToArray();
但是,通过调用 ToList 或ToArray,也可以将所有数据缓存在单个集合对象中。
掌握了 Linq 后 , 就是 是.NET里面的内置委托 Func
这里有篇介绍Func的文章 http://www.cnblogs.com/Gyoung/archive/2013/04/04/2997050.html
主要是最近看开源项目,发现 Func 用户多 , 就来学习下 , 并且 Func 可以与 接 lambda 表达式 , 所以就 放在这里一起做个简单的总结 :
public static void Main() { char[] separators = new char[] {' '}; Func<string, int, string[]> extract = (s, i) => i > 0 ? s.Split(separators, i) : s.Split(separators) ; string title = "The Scarlet Letter"; // Use Func instance to call ExtractWords method and display result foreach (string word in extract(title, 5)) Console.WriteLine(word); }
Func都是有返回类型的,如果我们的方法没有返回类型该怎么办呢?铛铛铛,这时Action就要粉墨登场了。
Action 委托:没有传入参数,也没有返回类型,即Void。如:
static void Main(string[] args) { Action say = SayHello; say(); } public static void SayHello( ) { Console.WriteLine("Say Hello"); }
Action<T> 委托:传入参数为T,没有返回类型。如:
static void Main(string[] args) { Action<string> say = SayHello; say("Hello"); } public static void SayHello(string word ) { Console.WriteLine(word); }
其实Action与Func的用法差不多,差别只是一个有返回类型,一个没有返回类型,当然Action也可以接匿名方法和Lambda表达式。
匿名方法:
static void Main(string[] args) { Action<string> say = delegate(string word) { Console.WriteLine(word); }; say("Hello Word"); }
Lambda表达式:
static void Main(string[] args) { Action<string> say = s => Console.WriteLine(s); say("Hello Word"); }