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