武林大师
class MartialArtsMaster
{
public int ID { get; set; }
public string Name { get; set; }
public int age { get; set; }
public string MenPai { get; set; }
public string KongFu { get; set; }
public int Level { get; set; }
public override string ToString()
{
return string.Format("ID:{0}, Name = :{1}, age =: {2}, MenPai = :{3}, KongFu = :{4}, Level = :{5}",ID,Name,age,MenPai,KongFu,Level);
}
}
武功类型
class Kongfu
{
public int ID { set; get; }
public string Name { get; set; }
public int Power { get; set; }
}
class Program
{
static void Main(string[] args)
{
var mastreList = new List<MartialArtsMaster>()
{
new MartialArtsMaster(){ID=1,Name="岳不群",age=58,MenPai="华山",KongFu="葵花宝典",Level=10},
new MartialArtsMaster(){ID=2,Name="风清扬",age=23,MenPai="华山",KongFu="孤独九剑",Level=7},
new MartialArtsMaster(){ID=3,Name="梅超风",age=45,MenPai="桃花岛",KongFu="九阴真经",Level=9}
};
var kongfu = new List<Kongfu>()
{
new Kongfu(){ID=1,Name="打狗棒法",Power=90},
new Kongfu(){ID=2,Name="降龙十八掌",Power=100},
new Kongfu(){ID=3,Name="葵花宝典",Power=90},
new Kongfu(){ID=4,Name="九阴真经",Power=80},
new Kongfu(){ID=5,Name="孤独九剑",Power=60},
};
//用LINQ查询级别大于8的
var res = from m in mastreList
where m.Level > 8
select m.Name;//表示把m的集合返回,可以选择要输出的内容 select m就全部输出
foreach (var temp in res)
{
Console.WriteLine(temp);
}
Console.ReadKey();
}
}
注意要加以下的步骤,才能正常输出。
public override string ToString()
{
return string.Format(“ID:{0}, Name = :{1}, age =: {2}, MenPai = :{3}, KongFu = :{4}, Level =
:{5}”,ID,Name,age,MenPai,KongFu,Level);
}
LINQ联合查询:
var res = from m in mastreList
where m.Level > 8&&m.Menpai==“丐帮”
select m.Name;
扩展方法写法
var res = mastreList.Where(m=>m.Level>8&&m.Menpai=="丐帮");
、
LINQ联合管理
var res = from m in mastreList
from k in kongfu
where m.KongFu==k.Name&&k.Power>85
select new { master = m, kongfu = k }; //选择要输出的内容
foreach (var temp in res)
{
Console.WriteLine(temp);
}
因为MartialArtsMaster没有武功的power,因此知道人名,想知道武功的power,就要从kongfu中去寻找。因此要用到LINQ联合管理。
上述程序的扩展方法用法:
var res=mastreList.SelectMany(m=>kongfu,(m,k)=>new{master=m,kongfu=k})
.Where(x=>x.master.KongFu==x.kongfu.Name&&x.kongfu.Power>85);
foreach (var temp in res)
{
Console.WriteLine(temp);
}
对结果进行排序 orderby
var res = from m in mastreList
where m.Level > 8
// orderby m.age 年龄从小到大排序
//orderby m.age,my.Level//先比较年龄,一样的话,再比较多等级排序
orderby m.age descending//按年龄从大到小排序
select m;//表示把m的集合返回,可以选择要输出的内容
foreach (var temp in res)
{
Console.WriteLine(temp);
}
上述代码可以这样写
var res = mastreList.Where(m => m.Level > 8).OrderBy(m => m.age);
多个字段时
var res = mastreList.Where(m => m.Level > 8).OrderBy(m => m.age).ThenBy(m => m.Level);
var res = mastreList.Where(m => m.Level > 8).OrderBy(m => m.age).OrderBy(m => m.Level);
这样是错误的,是先按等级排,又按年龄重新排了一次
Join On联合集合查询
var res = from m in mastreList
join k in kongfu on m.KongFu equals k.Name//用join on 做两个集合的连接
where k.Power>85
select new { master = m, kongFu = k };
foreach (var temp in res)
{
Console.WriteLine(temp);
}
对结果进行分组操作 into groups
var res = from k in kongfu
join m in mastreList on k.Name equals m.KongFu
into groups//按武功进行分组,看学什么武功的比较多
orderby groups.Count()
select new { kongfu = k, count = groups.Count() };
foreach (var temp in res)
{
Console.WriteLine(temp);
}
对结果进行分组操作 group by
var res = from m in mastreList
group m by m.MenPai into g//按门派进行分组
select new { count = g.Count(), key = g.Key };//g.Key表示按照哪个属性分的组
foreach (var temp in res)
{
Console.WriteLine(temp);
}
量词操作符any all
用来做判断是否存在或是否全是
> 存在: bool res = mastreList.Any(m => m.MenPai == "华山");
> 全是: bool res = mastreList.All(m => m.MenPai == "华山");