using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinqPractice
{
class Program
{
///
/// Linq查询
///
///
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}
};
var kongfuList = new List()
{
new Kongfu() {Id = 1, Name = "打狗棒法", Power = 90},
new Kongfu() {Id = 2, Name = "降龙十八掌", Power = 95},
new Kongfu() {Id = 3, Name = "葵花宝典", Power = 100},
new Kongfu() {Id = 4, Name = "独孤九剑", Power = 100},
new Kongfu() {Id = 5, Name = "九阴真经", Power = 100},
new Kongfu() {Id = 6, Name = "弹指神通", Power = 100}
};
//Linq查询:查询华山门派且级别大于5的角色
var tmplist = from m in masterList where m.Menpai == "华山" && m.Level > 5 select m;
//tmplist = masterList.Where(x => x.Menpai == "华山" && x.Level > 5);
//tmplist = masterList.Where(whereFunc);
//foreach(MartialArtsMaster master in tmplist)
//{
// Console.WriteLine(master.ToString());
//}
//Linq联合查询:查询功夫力量大于90且级别大于5的角色
tmplist = from m in masterList
from k in kongfuList
where m.Kongfu == k.Name && m.Level > 5 && k.Power > 90
select m;
foreach (MartialArtsMaster master in tmplist)
{
Console.WriteLine(master.ToString());
}
var res = masterList.SelectMany(m => kongfuList, (m, k) => new { master = m, kongfu = k })
.Where(x => x.master.Kongfu == x.kongfu.Name && x.kongfu.Power > 90);
// order by 排序
var res1 = from m in masterList
where m.Level > 8 && m.Menpai == "丐帮"
//orderby m.Age descending // 默认从小到大,加上descending从大到小
orderby m.Level, m.Age //按照多个字段进行排序,如果字段的属性相同,就按照第二个属性排序
select m;//表示m的结果结合返回
res1 = masterList.Where(m => m.Level > 8 && m.Menpai == "丐帮").OrderBy(m => m.Age);
res1 = masterList.Where(m => m.Level > 8).OrderBy(m => m.Level).ThenBy(m => m.Age);
//Join on 联合查询
var res2 = from m in masterList
// join...in... 表示要连接的表,on后面为连接条件,等于要用equals,不能用==
join k in kongfuList on m.Kongfu equals k.Name
where k.Power > 90
select new { master = m, kongfu = k };
//分组查询 into groups
var res3 = from k in kongfuList
join m in masterList on k.Name equals m.Kongfu
into groups
orderby groups.Count()
select new { kongfu = k, count = groups.Count() };
//按照自身字段分组 group x by x.property
var res4 = from m in masterList
group m by m.Kongfu
into g
select new { count = g.Count(), key = g.Key };//g.Key Key表示是按照那个属性分的组
}
static bool whereFunc(MartialArtsMaster master)
{
if (master.Menpai == "华山" && master.Level > 5) return true;
return false;
}
}
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 { get; set; }
public string Name { get; set; }
public int Power { get; set; }
public override string ToString()
{
return string.Format("Id: {0}, Name: {1}, Power: {2}", Id, Name, Power);
}
}
}