連表查詢
使用join可以將兩個數據源按照特定的條件連接到一起
內連接
查詢一個年份列表,查詢當前年份的賽車手冠軍和車隊冠軍
//得到所有冠軍賽車手
IList<Racer> champions_racers = Formula1.GetChampions();
//得到冠軍車隊
IList<Team> 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<Racer> champions_racers = Formula1.GetChampions();
//得到冠軍車隊
IList<Team> 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<Racer> champions_racers = Formula1.GetChampions();
//得到冠軍車隊
IList<Team> champions_team = Formula1.GetContructorChampions();
Func<string, IEnumerable<Racer>> 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<class1>()
{
new class1(){ name="Make"},
new class1(){ name="Phil"},
new class1(){ name="John"}
};
var class2 = new List<class2>()
{
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<Racer> champions_racers = Formula1.GetChampions();
//得到冠軍車隊
IList<Team> 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<Racer> champions_racers = Formula1.GetChampions();
//得到冠軍車隊
IList<Team> 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);
}