1. LINQ的读法:(1)lin k (2)lin q
2. LINQ的关键词:from, select, in, where, group by, order by …
3. LINQ的注意点:必须以select或者是group by 结束。
4. LINQ的语义:
from 临时变量 in 集合对象或数据库对象
where 条件表达式
[order by条件]
select 临时变量中被查询的值
[group by 条件]
LINQ的查询返回值的类型是临时变量的类型,可能是一个对象也可能是一个集合。并且LINQ的查询表达式是在最近一次创建对象时才被编译的。LINQ的查询一般跟var关键字一起联用 (什么是var?匿名对象) 。
例子:
int[] arr = new int[] { 8, 5, 89, 3, 56, 4, 1, 58 };
var m = from n in arr where n < 5 orderby n select n;
foreach (var n in m)
{ Console.WriteLine(n); }
例二:
static void Main(string[] args)
{
string [] languages = {"Java","C#","C++","Delphi","VB.net","VC.net","C++Builder","Kylix","Perl","Python"};
var query = from item in languages
orderby item
group item by item.Length into lengthGroups orderby lengthGroups.Key descending
select lengthGroups;
foreach (var item in query) {
Console.WriteLine("strings of length {0}",item.Key);
foreach (var val in item)
{ Console.WriteLine(val);
}
}
Console.ReadLine();
}