1、MartialArtsMaster类。
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; }
}
2、
class Program
{
static public List sd;
static void Main(string[] args)
{
//初始化武林高手
var masterList = new List(){
new MartialArtsMaster(){ Id = 1, Name = “黄蓉”, Age = 18, Menpai = “丐帮”, Kongfu = “打狗棒法”, Level = 9 },
new MartialArtsMaster(){ Id = 2, Name = “洪七公”, Age = 70, Menpai = “丐帮”, Kongfu = “打狗棒法”, Level = 10 },
new MartialArtsMaster(){ Id = 3, Name = “郭靖”, Age = 22, Menpai = “丐帮”, Kongfu = “降龙十八掌”,Level = 10 },
new MartialArtsMaster(){ Id = 4, Name = “任我行”, Age = 50, Menpai = “明教”, Kongfu = “葵花宝典”, Level = 1 },
new MartialArtsMaster(){ Id = 5, Name = “东方不败”,Age = 35, Menpai = “明教”, Kongfu = “葵花宝典”, Level = 10 },
new MartialArtsMaster(){ Id = 6, Name = “林平之”, Age = 23, Menpai = “华山”, Kongfu = “葵花宝典”, Level = 7 },
new MartialArtsMaster(){ Id = 7, Name = “岳不群”, Age = 50, Menpai = “华山”, Kongfu = “葵花宝典”, Level = 8 },
new MartialArtsMaster() { Id = 8, Name = “令狐冲”, Age = 23, Menpai = “华山”, Kongfu = “独孤九剑”, Level = 10 },
new MartialArtsMaster() { Id = 9, Name = “梅超风”, Age = 23, Menpai = “桃花岛”, Kongfu = “九阴真经”, Level = 8 },
new MartialArtsMaster() { Id =10, Name = “黄药师”, Age = 23, Menpai = “梅花岛”, Kongfu = “弹指神通”, Level = 10 },
new MartialArtsMaster() { Id = 11, Name = “风清扬”, Age = 23, Menpai = “华山”, Kongfu = “独孤九剑”, Level = 10 }
};
//使用linq查询。
var res = from m in masterList//from后面设置查询的集合
where m.Level > 8//where后面跟上查询的条件。
select m.Name;//表示m的结果结合返回。
foreach (var item in res)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
3、结果: