概述:
C# 3.0中加入的最为复杂的特性就是Linq查询表达式了,这使我们可直接采用类似于SQL的语法对集合进行查询,这就使我们可以享受到关系数据查询的强大功能
实例代码:
public static void Main()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// 定义匿名数组persons, 并为其赋初值
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var persons = new[]
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new
{ Name="Terry", Sex=false, Age=22 },
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new
{ Name="Martin", Sex=true, Age=30 },
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new
{ Name="Jerry", Sex=false, Age=24 },
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new
{ Name="Brog", Sex=false, Age=25 },
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new
{ Name="Tom", Sex=true, Age=20 }
};
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
执行简单Linq查询
检索所有年龄在24岁以内的人
查询结果放在results变量中
results变量的类型与数组persons相同
*/
var results = from p in persons
where p.Age <= 24
select p;
foreach (var person in results)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(person.Name);
}
Console.WriteLine();
// 定义匿名数组customers, 并为其赋初值
// 该数组是匿名类型的
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var customers = new[]
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Name="Terry", City="China", Orders=new[]
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new
{
OrderNo=0,
OrderName="C# Programming Language(Second Edition)",
OrderDate=new DateTime(2007,9, 5)
},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new
{
OrderNo=1,
OrderName="Head First Design Patterns(Chinese Edition)",
OrderDate=new DateTime(2007,9,15)
},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new
{
OrderNo=2,
OrderName="ASP.NET Unleashed 2.0(Chinese Edition)",
OrderDate=new DateTime(2007,09,18)
},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new
{
OrderNo=3,
OrderName="The C++ Programming Langauge(Special Edition)",
OrderDate=new DateTime(2002, 9, 20)
}
}
},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Name="Brog", City="China", Orders=new[]
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new
{
OrderNo=0,
OrderName="C# Programming Language(Second Edition)",
OrderDate=new DateTime(2007, 9, 15)
}
}
},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Name="Vicky", City="London", Orders=new[]
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new
{ OrderNo=0,
OrderName="C++ Programming Language(Special Edition)",
OrderDate=new DateTime(2007, 9, 20)
}
}
}
};
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
执行多重Linq查询
检索所在城市为中国, 且订单日期为2007年以后的所有记录
查询结果是一个匿名类型的数组
其中包含客户名, 订单号, 订单日期, 订单名四个字段
*/
var someCustomers = from c in customers
where c.City == "China"
from o in c.Orders
where o.OrderDate.Year >= 2007
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
select new
{ c.Name, o.OrderNo, o.OrderDate, o.OrderName };
foreach (var customer in someCustomers)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(
customer.Name + ", " + customer.OrderName + ", " +
customer.OrderDate.ToString("D")
);
}
}
运行结果: