using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class CSharp_学习03 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
List<MartialArtsMaster> mamList = new List<MartialArtsMaster>()
{
new MartialArtsMaster(){ ID = 1, Name = "黄蓉", Level = 18, Menpai = "丐帮", Gongfu = "打狗棒法" },
new MartialArtsMaster(){ ID = 2, Name = "洪七公", Level = 70, Menpai = "丐帮", Gongfu = "打狗棒法" },
new MartialArtsMaster(){ ID = 3, Name = "郭靖", Level = 22, Menpai = "丐帮", Gongfu = "降龙十八掌"},
new MartialArtsMaster(){ ID = 4, Name = "任我行", Level = 50, Menpai = "明教", Gongfu = "葵花宝典", },
new MartialArtsMaster(){ ID = 5, Name = "东方不败",Level = 35, Menpai = "明教", Gongfu = "葵花宝典", },
new MartialArtsMaster(){ ID = 6, Name = "林平之", Level = 23, Menpai = "华山", Gongfu = "葵花宝典", },
new MartialArtsMaster(){ ID = 7, Name = "岳不群", Level = 50, Menpai = "华山", Gongfu = "葵花宝典", },
new MartialArtsMaster(){ ID = 8, Name = "令狐冲", Level = 23, Menpai = "华山", Gongfu = "独孤九剑", },
new MartialArtsMaster(){ ID = 9, Name = "梅超风", Level = 23, Menpai = "桃花岛",Gongfu = "九阴真经", },
new MartialArtsMaster(){ ID =10, Name = "黄药师", Level = 23, Menpai = "梅花岛",Gongfu = "弹指神通", },
new MartialArtsMaster(){ ID = 11,Name = "风清扬", Level = 23, Menpai = "华山", Gongfu = "独孤九剑", }
};
List<Gongfu> gfList = new List<Gongfu>()
{
new Gongfu(){GongfuID=1, GongfuName="打狗棒法", Lethality=90f},
new Gongfu(){GongfuID=2, GongfuName="降龙十八掌", Lethality=95f},
new Gongfu(){GongfuID=3, GongfuName="葵花宝典", Lethality=100f},
new Gongfu(){ GongfuID= 4, GongfuName = "独孤九剑", Lethality = 100f },
new Gongfu(){ GongfuID = 5, GongfuName = "九阴真经", Lethality = 100f },
new Gongfu(){ GongfuID = 6, GongfuName = "弹指神通", Lethality = 100f }
};
//量词操作符 any all 判断集合中是否满足某个条件
bool b1 = mamList.Any(a => a.Menpai == "华山"); //是否有华山派的
print(b1); //输出true
bool b2 = mamList.All(a => a.Menpai == "华山"); //是否全都是华山派的
print(b2); //输出false
//使用Linq做查询 (表达式写法)
//var res = from m in mamList
//where m.Level > 25 //where后面跟查询的条件
//select m.Name; //返回m.Name结果的集合
//联合查询
var v = from m in mamList
from g in gfList
where m.Gongfu == g.GongfuName && g.Lethality >= 90f
orderby m.Level //对结果进行排序
//orderby m.Level descending //对结果进行倒序排序
orderby m.Level,m.ID //按照多个字段进行排序,如果字段的属性相同,就按照第二个属性进行排序
select m;
//把结果遍历出来
foreach (var item in v)
{
print(item);
}
}
}
/// <summary>
/// 武林高手类
/// </summary>
class MartialArtsMaster
{
public int ID { get; set; }
public string Name { get; set; }
public int Level { get; set; }
public string Menpai { get; set; } //门派
public string Gongfu { get; set; } //功夫
public override string ToString()
{
return string.Format("ID:{0},Name:{1},Level:{2},MenPai:{3},Gongfu{4}", ID, Name, Level, Menpai, Gongfu);
}
}
/// <summary>
/// 功夫类
/// </summary>
class Gongfu
{
public int GongfuID { get; set; }
public string GongfuName { get; set; }
public float Lethality { get; set; } //伤害
public override string ToString()
{
return string.Format("GongfuID:{0},GongfuName:{1},Lethality:{2}", GongfuID, GongfuName, Lethality);
}
}
随风_Linq示例
最新推荐文章于 2024-10-22 18:53:12 发布