定义:LINQ(读音link)代表语言集成查询(Language Integrated Query),是.NEt框架的扩展,它允许我们用SQL查询数据库的方式来查询数据的集合,使用它,你可以从数据库、程序对象的集合以及XML文档中查询数据
// See https://aka.ms/new-console-template for more information,
using System.Net.Http.Headers;
int[] scores = new int[] { 77,97,85,96 };
//IEnumerable与 var的区别,IEnumerable是语法糖
//IEnumerable<int>
// var scoreQuery =
// from score in scores
// where score > 80
// orderby score ascending
// select score;
//foreach (int score in scoreQuery)
//{
// Console.WriteLine(score);
//}
//lINQ初级示例
//string[] languages = { "java", "C#", "C++","Delphi","VB.net","VC.net","C++buILDER"};
//var query = from item in languages
// group item by item.Length into lenthGroups
// orderby lenthGroups.Key
// select lenthGroups;
//foreach (var item in query)
//{
// Console.WriteLine("string of length:{0}",item.Key);
// foreach (var str in item)
// {
// Console.WriteLine(str);
// }
//}
//协变与逆变
//Dog dog = new Dog();
//Animal animal = dog;
//List<Dog> dogs = new List<Dog>();
////OUT (协变,输出结果) ,in(逆变,作为参数),一个泛型参数标记为out,代表它是用来输出的,作为结果返回,结果有一个泛型参数标记为in,代表它是用来输入的,只能作为参数。
//List<Animal> animals2 = dogs.Select(d=>(Animal)d).ToList();
//IEnumerable<Dog> someDogs = new List<Dog>();
//IEnumerable<Animal> someAnimals = someDogs; //强制转换为合法
////Action<Animal>
//public abstract class Animal
//{
// public Animal() { }
//};
//public class Dog : Animal
//{
// public Dog() { }
//};
var master = new List<MartialArtsMaster>()
{
new MartialArtsMaster(){Id=1,Name ="黄蓉",Age= 18,MenPai ="丐帮",KungFu ="打狗棒法",Level=9 },
new MartialArtsMaster(){Id=2,Name ="洪七公",Age= 48,MenPai ="丐帮",KungFu ="打狗棒法",Level=10 },
new MartialArtsMaster(){Id=3,Name ="郭靖",Age= 18,MenPai ="丐帮",KungFu ="降龙十八掌",Level=10 },
new MartialArtsMaster(){Id=4,Name ="任我行",Age= 48,MenPai ="明教",KungFu ="吸星大法",Level=10 },
new MartialArtsMaster(){Id=5,Name ="东方不败",Age= 48,MenPai ="明教",KungFu ="葵花宝典",Level=10 }
};
var kungFu = new List<KungFu>()
{
new KungFu(){ KungFuId=1,KungFuName="打狗棒法",Lethality=90},
new KungFu(){ KungFuId=2,KungFuName="降龙十八掌",Lethality=95},
new KungFu(){ KungFuId=3,KungFuName="吸星大法",Lethality=95},
new KungFu(){ KungFuId=4,KungFuName="葵花宝典",Lethality=95}
};
var GaiBangMasterResult = "查询丐帮中功力高于80的大侠";
//LINQ
//var GaiBangMaster = from m in master
// where m.Level > 8 && m.MenPai == "丐帮"
// select m;
//foreach (var m in GaiBangMaster)
//{
// GaiBangMasterResult = m.Id + " " + m.Name + " " + m.Age + " " + m.MenPai + " " + m.KungFu + " " + m.Level + "" + "\n";
// Console.Write(GaiBangMasterResult);
//}
//lambda
//var GaiBangMasterMethod = master.Where(m=>m.Level >8 && m.MenPai=="丐帮");
//foreach (var m in GaiBangMasterMethod)
//{
// GaiBangMasterResult = m.Id + " " + m.Name + " " + m.Age + " " + m.MenPai + " " + m.KungFu + " " + m.Level + "" + "\n";
// Console.Write(GaiBangMasterResult);
//}
//过滤
//var masterKungFu = from m in master
// from k in kungFu
// where k.Lethality > 90 && m.KungFu == k.KungFuName
// orderby m.Level
// select m.Id + " " + m.Name + " " + m.Age + " " + m.MenPai + " " + m.KungFu + " " + m.Level + "";
//var masterKungFuResult = "过滤功夫杀伤力大于90的大侠:\n";
//foreach (var m in masterKungFu)
//{
// masterKungFuResult += m.ToString()+" " + "\n";
// Console.WriteLine(masterKungFuResult);
//}
//扩展方法
//var masterKungFuMethod = master.SelectMany(k => kungFu, (m, k) => new { mt = m, kf = k })
// .Where(x => x.kf.Lethality > 90 && x.mt.KungFu == x.kf.KungFuName)
// .OrderBy(m => m.mt.Level)
// .Select(m => m.mt.Id + " " + m.mt.Name + " " + m.mt.Age + " " + m.mt.MenPai + " " + m.mt.KungFu + " " + m.mt.Level + " ");
//string masterKungFuMehtodResult = "过滤功夫杀伤力大于90的大侠:\n";
//foreach (var m in masterKungFuMethod)
//{
// masterKungFuMehtodResult += m.ToString() + " " + "\n";
// Console.WriteLine(masterKungFuMehtodResult);
//}
//int i = 0;
//var topMaster = from m in master
// from k in kungFu
// where k.KungFuName == m.KungFu
// orderby m.Level * k.Lethality descending , m.Age , m.Name
// select m.Id + " " + m.Name + " " + m.Age + " " + m.MenPai + " " + m.KungFu + " " + m.Level + " " + k.Lethality+" "+ (++i);
//string topMasterResult = "高手攻击力排行榜\n";
//foreach (var m in topMaster)
//{
// topMasterResult += m.ToString() + " " + "\n";
// Console.WriteLine(topMasterResult);
//}
//var topMasterMethod = master.SelectMany(k=>kungFu,(m,k)=>new { mt=m,kf =k})
// .Where(x=>x.mt.KungFu == x.kf.KungFuName)
// .OrderBy(m=>m.mt.Level * m.kf.Lethality)
// .ThenBy(m=>m.mt.Name)
// .ThenBy(m=>m.mt.Age)
// .Select(m => m.mt.Name + " " + m.mt.Name + " " + m.mt.Age + " " + m.mt.MenPai + " " + m.mt.KungFu + " " + m.mt.Level + " " + m.kf.Lethality + " " + (++i));
//string topMasterResult = "高手攻击力排行榜\n";
//foreach (var m in topMasterMethod)
//{
// topMasterResult += m.ToString() + " " + "\n";
// Console.WriteLine(topMasterResult);
//}
//
//int i = 1;
//var masterTop = master.Where(x => x.Level > 8)
// .OrderByDescending(x => x.Level)
// .Select(x=> new { id = x.Id, Name = x.Name, masterKungFu = x.KungFu, Level = x.Level, Top = (++i) });
//int li = 1;
//var kungFuTop = from k in kungFu
// where k.Lethality >90
// orderby k.Lethality descending
// select new { kungFuId = k.KungFuId, kungFuName = k.KungFuName, Lethality = k.Lethality, kungFuTop = (li++)};
//var masterLehtalityTop = from m in masterTop
// join k in kungFuTop on m.masterKungFu equals k.kungFuName
// orderby m.Level * k.Lethality descending
// select new { Id = m.id, Name = m.Name, Kungfu = m.masterKungFu, Level = m.Level, Kill = m.Level * k.Lethality };
//var masterLethalityTopMethod = masterTop.Join(kungFuTop, m=>m.masterKungFu, k => k.kungFuName, (m, k) => new { id=m.id,Name=m.Name,KungFu=m.masterKungFu,Level = m.Level, Kill = m.Level * k.Lethality})
// .OrderByDescending(m=>m.Kill);
//Console.WriteLine("通过对象关联,返回新的对象的高手的杀伤力:");
//foreach (var m in masterLehtalityTop)
//{
// Console.WriteLine(m.Id+" "+ m.Name +" "+ m.Kungfu+" "+m.Level+" "+ m.Kill+"\n");
//}
//Console.WriteLine("通过对象关联,返回新的对象的高手的杀伤力:");
//foreach (var m in masterLethalityTopMethod)
//{
// Console.WriteLine(m.id + " " + m.Name + " " + m.KungFu + " " + m.Level + " " + m.Kill + "\n");
//}
//使用Join和GroupJoin的区别,使用Join
//Console.WriteLine("使用Join和GroupJoin的区别,使用Join:\n");
//var JoinOnGroupJoin = from k in kungFu
// join m in master on k.KungFuName equals m.KungFu
// orderby k.KungFuId
// select new
// {
// KungFuId = k.KungFuId,
// KungFuName = k.KungFuName,
// Lethality = k.Lethality,
// Master = m.Name
// };
//foreach (var m in JoinOnGroupJoin)
//{
// Console.WriteLine(m.KungFuId+" "+ m.KungFuName +" "+ m.Lethality +" "+ m.Master + "\n");
//}
//Console.WriteLine("使用Join和GroupJoin的区别,使用扩展方法:\n");
//var JoinOnGroupJoinMethod = kungFu.GroupJoin(master,k=>k.KungFuName,m=>m.KungFu,(k,m)=>new { k.KungFuId,k.KungFuName,k.Lethality,Count = m.Count()})
// .OrderBy(k=>k.KungFuId);
//foreach (var m in JoinOnGroupJoinMethod)
//{
// Console.WriteLine(m.KungFuId+" "+ m.KungFuName +" "+ m.Lethality +" "+ m.Count+"\n");
//}
//分组
master.Add(new MartialArtsMaster() { Id=8,Name ="令狐冲",Age =23, MenPai ="华山",KungFu="独孤九剑",Level= 10});
master.Add(new MartialArtsMaster() { Id = 9, Name = "梅超风", Age = 23, MenPai = "桃花岛", KungFu = "九阴真经", Level = 8 });
master.Add(new MartialArtsMaster() { Id = 10, Name = "黄药师", Age = 44, MenPai = "桃花岛", KungFu = "弹指神通", Level = 10 });
master.Add(new MartialArtsMaster() { Id = 11, Name = "风清扬", Age = 23, MenPai = "华山", KungFu = "独孤九剑", Level = 10 });
kungFu.Add(new KungFu() {KungFuId=4,KungFuName= "独孤九剑", Lethality=100});
kungFu.Add(new KungFu() { KungFuId = 5, KungFuName = "弹指神通", Lethality = 100 });
kungFu.Add(new KungFu() { KungFuId = 6, KungFuName = "九阴真经", Lethality = 100 });
Console.WriteLine("武学分组");
//var masterItems = from k in kungFu
// join m in master on k.KungFuName equals m.KungFu
// into groups
// orderby groups.Count() descending
// select new
// {
// KungfuId = k.KungFuId,
// kungFuName = k.KungFuName,
// Lethality = k.Lethality,
// Count = groups.Count()
// };
//foreach (var m in masterItems)
//{
// Console.WriteLine(m.KungfuId +" "+ m.kungFuName +" "+ m.Lethality +" "+ m.Count+ "\n");
//}
//var masterItemsMothed = kungFu.GroupJoin(master,k=> k.KungFuName, m=>m.KungFu ,(k,m)=> new { k.KungFuId,k.KungFuName,k.Lethality,Count =m.Count() })
// .OrderByDescending(k=>k.Count);
//foreach (var m in masterItemsMothed)
//{
// Console.WriteLine(m.KungFuId +" "+ m.KungFuName+" "+ m.Lethality+" "+ m.Count +"\n");
//}
//分组操作符
//var GroupItems = from m in master
// group m by m.MenPai into g
// orderby g.Key
// select new
// {
// Menpai = g.Key,
// Count = g.Count(),
// };
//Console.WriteLine("分组表达式:\n");
//foreach (var m in GroupItems)
//{
// Console.WriteLine(m.Menpai+" "+ m.Count +"\n");
//}
//扩展方法
//var GroupItemsMethod = master.GroupBy(m => m.MenPai, (k, m) => new { Menpai =k,Count =m.Count()});
//Console.WriteLine("扩展方法\n");
//foreach (var m in GroupItemsMethod)
//{
// Console.WriteLine(m.Menpai +" "+ m.Count +"\n");
//}
//var AnyItems = from m in master
// where m.KungFu == "葵花宝典"
// select new { m.Name, m.MenPai, m.KungFu };
//Console.WriteLine("葵花大侠:\n");
//foreach (var n in AnyItems)
//{
// Console.WriteLine(n.Name +" "+ n.MenPai +" "+ n.KungFu+"\n");
//}
//var any = AnyItems.Any(m=>m.MenPai=="明教");
//Console.WriteLine("明教葵花大侠:"+ any);
//var all = AnyItems.All(m=>m.MenPai=="明教");
//Console.WriteLine("是否练习葵花的人全是明教人"+all);
//判断是否包含
//var OuYangFeng = new MartialArtsMaster() { Id =13,Name="欧阳锋",Age= 50,MenPai="白陀山庄",KungFu="蛤蟆功",Level=10};
//var HuanYaoShi = master[4];
//var IsOuYangFeng = master.Contains(OuYangFeng);
//var IsHuangYaoShi = master.Contains(HuanYaoShi as MartialArtsMaster);
//Console.WriteLine("大侠里是否有欧阳锋"+IsOuYangFeng);
//Console.WriteLine("大侠里是否有黄药师" + IsHuangYaoShi);
//分页
//master.Add(new MartialArtsMaster() { Id = 12, Name = "萧峰", Age = 33, MenPai = "丐帮", KungFu = "降龙十八掌", Level = 10 });
//master.Add(new MartialArtsMaster() { Id = 13, Name = "段誉", Age = 23, MenPai = "天龙寺", KungFu = "六脉神剑", Level = 7 });
//master.Add(new MartialArtsMaster() { Id = 14, Name = "虚竹", Age = 33, MenPai = "逍遥派", KungFu = "北冥神功", Level = 9 });
//master.Add(new MartialArtsMaster() { Id = 15, Name = "方正大师", Age = 33, MenPai = "少林", KungFu = "七十二绝技", Level = 10 });
//master.Add(new MartialArtsMaster() { Id = 16, Name = "杨过", Age = 23, MenPai = "古墓派", KungFu = "玉女心经", Level = 10 });
//master.Add(new MartialArtsMaster() { Id = 17, Name = "小龙女", Age = 23, MenPai = "古墓派", KungFu = "玉女心经", Level = 10 });
//kungFu.Add(new KungFu() { KungFuId = 7, KungFuName = "六脉神剑", Lethality = 100 });
//kungFu.Add(new KungFu() { KungFuId = 8, KungFuName = "北冥神功", Lethality = 100 });
//kungFu.Add(new KungFu() { KungFuId = 9, KungFuName = "七十二绝技", Lethality = 100 });
//kungFu.Add(new KungFu() { KungFuId = 10, KungFuName = "玉女心经", Lethality = 100 });
//int pageSize = 5;
//int pageNumber = (int)Math.Ceiling(master.Count()/(double)pageSize); //计算总页数
//Console.WriteLine("使用分区操作符分页:\n");
//Console.WriteLine("大侠总数"+master.Count()+ "总页数:"+pageNumber+"每页"+pageSize+"\n");
//for (int i = 0; i < pageNumber; i++)
//{
// var pageMarster = (from m in master
// join k in kungFu on m.KungFu equals k.KungFuName
// orderby m.Level * k.Lethality descending
// select new
// {
// m.Name, m.MenPai , m.KungFu, k.Lethality, m.Level ,Kill= m.Level *k.Lethality
// }).Skip(i*pageSize).Take(pageSize);
// Console.WriteLine("姓名 门派 武功 杀伤力 修炼等级 总武力");
// foreach (var m in pageMarster)
// {
// Console.WriteLine(m.Name +" "+ m.MenPai +" "+ m.KungFu+" "+ m.Kill+" "+ m.Level+" "+ m.Kill+"\n");
// }
//}
//集合操作符 Union, 并集且没有重复元素,Concact:返回两个并集,Except:差集,返回只出现在一个序列中的元素
//var ItemIntersect = (from m in master
// where m.MenPai == "华山" || m.MenPai == "明教"
// select m).Intersect(from m in master where m.KungFu =="葵花宝典" select m);
//foreach (var m in ItemIntersect)
//{
// Console.WriteLine(m.Name +" "+ m.MenPai +" "+ m.KungFu+"\n");
//}
//Count 元素总量, LongCount 元素总量,Sum, 元素总和, Max最大值,Min 最小值
int[] number = { 1,2,3};
int x = number.Aggregate((prod,n)=>prod +n);
int y = number.Aggregate(0,(prod,n)=>prod +n);
int z = number.Aggregate(0,(prop,n)=>prop+n,r => r*2);
Console.WriteLine(x+" "+y+" "+z);
//武学
public class KungFu
{
public int KungFuId { get; set; }
public string KungFuName { get; set; }
public int Lethality { get; set; }
}
//武林高手
public class MartialArtsMaster
{
public int Id
{ get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string MenPai { get; set; }
public string KungFu { get; set; }
public int Level { get; set; }
}
教学视频,转载自:https://www.bilibili.com/video/BV1nk4y1k7Nw?p=1&vd_source=8b9d45e48f2b8f1ea661d0b2504464fa
标签:Name,Level,Lethality,KungFu,LINQ,new,var,操作 From: https://www.cnblogs.com/KevinSteven/p/17412407.html