LINQ数据查询
程序员文章站
2022-04-15 14:37:06
...
首先需要武林高手和武学类
武林高手类
/// <summary>
/// 武林高手类
/// </summary>
class MartialArtsMaster
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string MeiPai { get; set; }
public string GongFu { get; set; }
public int Level { get; set; }
public override string ToString()
{
return string.Format("Id:{0},Name:{1},Age:{2},MeiPai:{3},GongFu:{4},Level:{5}", Id, Name, Age, MeiPai, GongFu, Level);
}
}
武学(功夫)类
/// <summary>
/// 武学类
/// </summary>
class GongFu
{
public int Id { get; set; }
public string Name { get; set; }
public int Harm { get; set; }
public override string ToString()
{
return string.Format("Id:{0},Name:{1},Harm:{2}", Id,Name,Harm);
}
}
初始化武林高手和武学类数据
class Program
{
static List<MartialArtsMaster> masterList;//武林高手集合
static List<GongFu> gongFuList; // 武学集合
static void Main(string[] args)
{
InItData();
Console.ReadKey();
}
/// <summary>
/// 初始化数据
/// </summary>
public static void InItData()
{
//初始化武林高手集合
masterList = new List<MartialArtsMaster>()
{
new MartialArtsMaster(){Id = 1,Name = "黄蓉",Age = 18,MeiPai = "丐帮", GongFu = "打狗棒法", Level = 9 },
new MartialArtsMaster(){Id = 2,Name = "洪七公",Age = 70,MeiPai = "丐帮", GongFu = "打狗棒法", Level = 10 },
new MartialArtsMaster(){Id = 3,Name = "郭靖",Age = 22,MeiPai = "丐帮", GongFu = "降龙十八掌",Level = 10 },
new MartialArtsMaster(){Id = 4,Name = "任我行",Age = 50,MeiPai = "明教", GongFu = "葵花宝典", Level = 1 },
new MartialArtsMaster(){Id = 5,Name = "东方不败",Age = 35,MeiPai = "明教", GongFu = "葵花宝典", Level = 10 },
new MartialArtsMaster(){Id = 6,Name = "林平之",Age = 23,MeiPai = "华山", GongFu = "葵花宝典", Level = 7 },
new MartialArtsMaster(){Id = 7,Name = "岳不群",Age = 50,MeiPai = "华山", GongFu = "葵花宝典", Level = 8 },
new MartialArtsMaster(){Id = 8,Name = "令狐冲",Age = 23,MeiPai = "华山", GongFu = "独孤九剑", Level = 10 },
new MartialArtsMaster(){Id = 9,Name = "梅超风",Age = 23,MeiPai = "桃花岛", GongFu = "九阴真经", Level = 8 },
new MartialArtsMaster(){Id = 10,Name = "黄药师",Age = 23,MeiPai = "梅花岛", GongFu = "弹指神通", Level = 10 },
new MartialArtsMaster(){Id = 11,Name = "风清扬",Age = 23,MeiPai = "华山", GongFu = "独孤九剑", Level = 10 }
};
// 初始化武学集合
gongFuList = new List<GongFu>()
{
new GongFu(){Id=1, Name="打狗棒法", Harm=90},
new GongFu(){Id=2, Name="降龙十八掌", Harm=95},
new GongFu(){Id=3, Name="葵花宝典", Harm=100},
new GongFu() {Id= 4, Name = "独孤九剑", Harm = 100 },
new GongFu() {Id = 5, Name = "九阴真经", Harm = 100 },
new GongFu() {Id = 6, Name = "弹指神通", Harm = 100 }
};
}
LINQ表达式查询(单一查询)
public static void SoleLINQInquire()
{
// 使用LINQ,查询武学等级大于8的武林高手(表达式写法)
var res = from m in masterList // from后面设置需要查询的集合
where m.Level > 8 // where后面跟上查询条件(单一条件)
// where m.Level > 8 && m.MeiPai == "丐帮" // where后面跟上查询条件(多个条件判断)
select m; // select返回符合查询条件集合的结果
// select m.Name; // select返回符合查询条件结果的所有名字
foreach (var item in res)
{
Console.WriteLine(item);
}
}
LINQ方法查询(单一查询)
/// <summary>
/// 单一LINQ方法查询
/// </summary>
public static void SoleLINQExtendInquire()
{
// 扩展方法
//var res1 = masterList.Where(m => m.Level > 8);// 单个条件判断
var res = masterList.Where(m => m.Level > 8 && m.MeiPai == "丐帮"); // 多个条件判断
foreach (var item in res)
{
Console.WriteLine(item);
}
}
联合LINQ表达式查询
/// <summary>
/// 联合LINQ表达式查询
/// </summary>
public static void UniteLINQInquire()
{
var res = from m in masterList
from k in gongFuList
where m.GongFu == k.Name && k.Harm > 90 // 武学相同并且伤害大于等于90
select m; // 输出符合条件的武林高手
//select new { masterList = m, kongFuList = k }; // 输出所有武林高手和武学
foreach (var item in res)
{
Console.WriteLine(item);
}
}
联合LINQ方法查询
/// <summary>
/// 联合LINQ方法查询
/// </summary>
public static void UniteLINQExtendInquire()
{
var res = masterList.SelectMany(m => gongFuList,(m,k) => new {masterList = m,kongFuList = k})
.Where(x =>x.masterList.GongFu == x.kongFuList.Name && x.kongFuList.Harm > 90);
foreach (var item in res)
{
Console.WriteLine(item);
}
}
LINQ表达式排序
/// <summary>
/// 表达式排序
/// </summary>
public static void LINQSort()
{
var res = from m in masterList
where m.Level > 8 && m.MeiPai == "丐帮"
orderby m.Level,m.Age // 按照多个字段进行排序,如果第一个字段的属性相同,就按照第二个属性排序
select m;
foreach (var item in res)
{
Console.WriteLine(item);
}
}
LINQ方法排序
/// <summary>
/// 方法排序
/// </summary>
public static void LINQExtendSort()
{
//var res = masterList.Where(m => m.Level > 8 && m.MeiPai == "丐帮").OrderBy(m => m.Level);
var res = masterList.Where(m => m.Level > 8 && m.MeiPai == "丐帮").OrderBy(m => m.Level).ThenBy(m => m.Age);
foreach (var item in res)
{
Console.WriteLine(item);
}
}
LINQ Join on集合联合查询
/// <summary>
/// Join on集合联合查询
/// </summary>
public static void LINQJoinNo()
{
var res = from m in masterList
join k in gongFuList on m.GongFu equals k.Name // join后面跟需要关联的集合,on后面是关联的条件
where k.Harm > 90
select new { masterList = m, kongFuList = k };
foreach (var item in res)
{
Console.WriteLine(item);
}
}
LINQ 分组查询
/// <summary>
/// 分组查询(把武林高手按照所学功夫分类,看那种功夫修理的人数最多)
/// </summary>
public static void LINQGroupingSort()
{
var res = from k in gongFuList
join m in masterList on k.Name equals m.GongFu
into groups // 分组信息
orderby groups.Count()// 按分组的数量排序
select new { GongFu = k, count = groups.Count() };
foreach (var item in res)
{
Console.WriteLine(item);
}
}
LINQ 按字段分组
// 按照字段分组
public static void LINQFields()
{
var res = from m in masterList
group m by m.MeiPai into g
orderby g.Count()
select new { g.Key, count = g.Count() }; // key表示是按照那个属性分的组
foreach (var item in res)
{
Console.WriteLine(item);
}
}
LINQ 量词操作符
/// <summary>
/// 量词操作符
/// </summary>
public static void LINQMeasure()
{
// 量词操作符any all判断集合中是否满足某个条件,只要有一个满足就会返回true
bool res = masterList.Any(m => m.MeiPai == "丐帮");
// 都得满足判断条件才会返回true
bool res1 = masterList.All(m => m.MeiPai == "丐帮");
Console.WriteLine(res);
Console.WriteLine(res1);
}
上一篇: L1-033 出生年
下一篇: Java-HashSet原理