使用join可以将两个数据源按照特定的条件连接到一起
查询一个年份列表,查询当前年份的赛车手冠军和车队冠军
//得到所有冠军赛车手
IList champions_racers = Formula1.GetChampions();
//得到冠军车队
IList champions_team = Formula1.GetContructorChampions();
var query = from r in champions_racers
from c in r.Years
select new {
year=c,
name=r.FirstName+" "+r.LastName
};
var query1 = from r in champions_team
from c in r.Years
select new
{
year = c,
name = r.Name
};
var query2 = (from r in query
join t in query1 on r.year equals t.year
orderby r.year
select new { r.year,rname=r.name,tname=t.name }).Take(10);
foreach (var item in query2)
{
Console.WriteLine("year={0},rname={1},tname={2}", item.year,item.rname,item.tname);
}
如赛车手比车队设立冠军的年份要早,可能某个年份只有赛车手冠军没有车队冠军,这时候需要左连接查询。
//得到所有冠军赛车手
IList champions_racers = Formula1.GetChampions();
//得到冠军车队
IList champions_team = Formula1.GetContructorChampions();
var query = from r in champions_racers
from c in r.Years
select new {
year=c,
name=r.FirstName+" "+r.LastName
};
var query1 = from r in champions_team
from c in r.Years
select new
{
year = c,
name = r.Name
};
var query2 = (from r in query
join t in query1 on r.year equals t.year into rt
from nrt in rt.DefaultIfEmpty()
orderby r.year
select new { r.year, rname = r.name, tname = nrt == null ? "No" : nrt.name }).Take(10);
foreach (var item in query2)
{
Console.WriteLine("year={0},rname={1},tname={2}", item.year,item.rname,item.tname);
}
使用into将结果放入新的临时表rt中,在使用rt.DefaultIfEmpty,返回一个新序列元素 nrt,赋值时判断nrt是否为空,既可。
linq只支持左连接,如要右连接,将query和query1调换位置
可以使用扩展方法Intersect、Union、Distinct、Except等
如:查询同时拥有法拉利和迈凯伦获得冠军的车手。
//得到所有冠军赛车手
IList champions_racers = Formula1.GetChampions();
//得到冠军车队
IList champions_team = Formula1.GetContructorChampions();
Func<string, IEnumerable> RacerByCar = carname => from cr in champions_racers
from crcars in cr.Cars
where crcars == carname
orderby cr.LastName
select cr;
foreach (var item in RacerByCar("Ferrari").Except(RacerByCar("McLaren")))
{
Console.WriteLine(item.ToString("A"));
}
zip是.net4.0新增的,允许两个相关的数据源合并为一个
var class1 = new List()
{
new class1(){ name="Make"},
new class1(){ name="Phil"},
new class1(){ name="John"}
};
var class2 = new List()
{
new class2(){ name="Vanwall", age=18},
new class2(){ name="Cooper", age=15},
new class2(){ name="Ferrari", age=28},
new class2(){ name="BRM", age=14},
};
var result = class1.Zip(class2, (r, t) => string.Format("class1-name={0},class2-name={1},age={2}", r.name, t.name, t.age));
foreach (var item in result)
{
Console.WriteLine(item);
}
skip和Task联合使用即可实现分页效果
//得到所有冠军赛车手
IList champions_racers = Formula1.GetChampions();
//得到冠军车队
IList champions_team = Formula1.GetContructorChampions();
var query = (from cr in champions_racers select cr).Skip(1*5).Take(5);
foreach (var item in query)
{
Console.WriteLine(item.ToString());
}
转换有聚合函数:sum 、count、min、max、average、aggregate等
转换操作符:ToList、ToLookup等等
生成操作符:Range、Empty等等
返回获得冠军超过3次的赛车手
//得到所有冠军赛车手
IList champions_racers = Formula1.GetChampions();
//得到冠军车队
IList champions_team = Formula1.GetContructorChampions();
var query = from cr in champions_racers
let numberYears = cr.Years.Count()
where numberYears >= 3
orderby numberYears descending,cr.LastName
select new
{
name = cr.FirstName + " " + cr.LastName,
numberYears=numberYears
};
foreach (var item in query)
{
Console.WriteLine(string.Format("{0}\t{1}",item.name,item.numberYears));
}
使用 let 定义一个变量
ToList、ToLookup、ToArray等不做过多介绍
range、Repeat等等,使用很简单
foreach (var item in Enumerable.Range(1,20))
{
Console.WriteLine(item);
}