Create a console program to print the results:
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqStudyDemo
{
class Program
{
static void Main(string[] args)
{
//初始化武林高手
var masters = new List<MartialArtsMaster>(){
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 }
};
//初始化武学
var kongfus = new List<Kongfu>() {
new Kongfu(){KongfuId=1,KongfuName="打狗棒法",Lethality=90 },
new Kongfu(){KongfuId=2,KongfuName="降龙十八掌",Lethality=95 },
new Kongfu(){KongfuId=3,KongfuName="吸星大法",Lethality=99 },
new Kongfu(){KongfuId=4,KongfuName="葵花宝典",Lethality=100 }
};
//方法一(使用linq中的查询表达式)
var GaiBangMasters = from m in masters
where m.Level > 9 && m.Menpai == "丐帮"
select m;
foreach (var item in GaiBangMasters)
{
var GaiBangResult = item.Id + " " + item.Name + " " + item.Menpai + " " + item.Kongfu + " " + item.Level;
Console.WriteLine(GaiBangResult);
}
Console.WriteLine("---------------------------------------");
//方法二(使用linq中的lambda表达式)
var GaiBangMasters2 = masters.Where(m => m.Level > 9 && m.Menpai == "丐帮");
foreach (var item in GaiBangMasters2)
{
var GaiBangResult = item.Id + " " + item.Name + " " + item.Menpai + " " + item.Kongfu + " " + item.Level;
Console.WriteLine(GaiBangResult);
}
}
}
/// <summary>
/// 武林高手
/// </summary>
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; }
}
/// <summary>
/// 武学
/// </summary>
class Kongfu
{
public int KongfuId { get; set; }
public string KongfuName { get; set; }
public int Lethality { get; set; }
}
}
Results of the:
The execution results of Method 1 and Method 2 are the same, both of which query the information of martial arts masters in the Beggar Gang who are above level 9 in martial arts.
Example 2: Filter information based on conditions
Console.WriteLine("杀伤力大于90的武林高手信息:");
var GaiBangMasters3 = from m in masters
from k in kongfus
where m.Kongfu == k.KongfuName && k.Lethality > 90
orderby m.Level
select m.Id + " " + m.Name + " " + m.Menpai + " " + m.Kongfu + " " + m.Level + " " + k.Lethality;
foreach (var item in GaiBangMasters3)
{
Console.WriteLine(item);
}
Results of the:
Example 3: Statistics of martial arts rankings
Console.WriteLine("武林排行榜:");
var GaiBangMasters4 = from m in masters
from k in kongfus
where m.Kongfu == k.KongfuName
orderby m.Level * k.Lethality descending, m.Name
select m.Id + " " + m.Name + " " + m.Menpai + " " + m.Kongfu + " " + m.Level * k.Lethality;
foreach (var item in GaiBangMasters4)
{
Console.WriteLine(item);
}
Query statements can also be associated with join:
var GaiBangMasters4 = from m in masters
join k in kongfus on m.Kongfu equals k.KongfuName
orderby m.Level * k.Lethality descending, m.Name
select m.Id + " " + m.Name + " " + m.Menpai + " " + m.Kongfu + " " + m.Level * k.Lethality;
Results of the:
Example 4: Query the strongest person among the martial arts heroes
Console.WriteLine("武林最强王者:");
var GaiBangMasters4 = (from m in masters
join k in kongfus on m.Kongfu equals k.KongfuName
orderby m.Level * k.Lethality descending, m.Name
select m.Id + " " + m.Name + " " + m.Menpai + " " + m.Kongfu + " " + m.Level * k.Lethality).FirstOrDefault();
Console.WriteLine(GaiBangMasters4);
Results of the:
Example 5: Count the number of people in each sect
Console.WriteLine("统计各门派的人数:");
var GaiBangMasters5 = from m in masters
group m by m.Menpai into g
orderby g.Key
select new
{
menpai = g.Key,
count = g.Count()
};
foreach (var item in GaiBangMasters5)
{
Console.WriteLine("门派:" + item.menpai + ", 人数:" + item.count);
}
Results of the:
Example 6: The use of quantifiers in linq (any, all, contains)
var kxbd = from m in masters
where m.Kongfu == "葵花宝典"
select new { m.Name, m.Menpai, m.Kongfu };
Console.WriteLine("练习葵花宝典的大侠:");
foreach (var item in kxbd)
{
Console.WriteLine(item.Name + " " + item.Menpai + " " + item.Kongfu);
}
Console.WriteLine();
var isHaveMJ = kxbd.Any(m => m.Menpai == "明教");
Console.WriteLine("练习葵花宝典的是否有明教中人? " + (isHaveMJ ? "有" : "没有"));
Console.WriteLine();
var isAllMJ = kxbd.All(m => m.Menpai == "明教");
Console.WriteLine("练习葵花宝典的都是明教中人? " + (isAllMJ ? "是" : "不是"));
Console.WriteLine();
var ouYangFeng = new MartialArtsMaster() { Id = 8, Name = "欧阳锋", Age = 40, Menpai = "白驼山庄", Kongfu = "蛤蟆功", Level = 10 };
var huangRong = masters[0];
var isOuYangFeng = masters.Contains(ouYangFeng);
var isHuangRonng = masters.Contains(huangRong);
Console.WriteLine("欧阳锋是否是大侠? " + (isOuYangFeng ? "是" : "不是"));
Console.WriteLine("黄蓉是否是大侠? " + (isHuangRonng ? "是" : "不是"));
Results of the:
Example 7: Complete the paging operation
int pageSize = 3;//每页显示记录数
int totalPage = (int)Math.Ceiling(masters.Count() / (double)pageSize);//总页数
for (int i = 0; i < totalPage; i++)
{
var pageMarster = (
from m in masters
join k in kongfus on m.Kongfu equals k.KongfuName
orderby m.Level * k.Lethality descending, m.Name
select new
{
m.Name,
m.Menpai,
m.Kongfu,
kill = m.Level * k.Lethality
}).Skip(pageSize * i).Take(pageSize);
Console.WriteLine("第" + (i + 1) + "页");
Console.WriteLine("姓名 门派 武功 杀伤力");
foreach (var item in pageMarster)
{
Console.WriteLine(item.Name + " " + item.Menpai + " " + item.Kongfu + " " + item.kill);
}
Console.WriteLine();
}
Results of the:
Example 8: The use of set operators in linq (Intersect, Union, Concat, Except)
Console.WriteLine("集合一:明教派的大侠");
var result1 =
from m1 in masters
where m1.Menpai == "明教"
select m1;
foreach (var item in result1)
{
Console.WriteLine(item.Name + " " + item.Menpai + " " + item.Kongfu);
}
Console.WriteLine();
Console.WriteLine("集合二:练习葵花宝典的大侠");
var result2 =
from m2 in masters
where m2.Kongfu == "葵花宝典"
select m2;
foreach (var item in result2)
{
Console.WriteLine(item.Name + " " + item.Menpai + " " + item.Kongfu);
}
Console.WriteLine();
Console.WriteLine("集合操作符:Intersect,求两个集合的交集");
var selectMaster1 = (
from m1 in masters
where m1.Menpai == "明教"
select m1
).Intersect(
from m2 in masters
where m2.Kongfu == "葵花宝典"
select m2
);
foreach (var item in selectMaster1)
{
Console.WriteLine(item.Name + " " + item.Menpai + " " + item.Kongfu);
}
Console.WriteLine();
Console.WriteLine("集合操作符:Union,求两个集合的并集,去重");
var selectMaster2 = (
from m1 in masters
where m1.Menpai == "明教"
select m1
).Union(
from m2 in masters
where m2.Kongfu == "葵花宝典"
select m2
);
foreach (var item in selectMaster2)
{
Console.WriteLine(item.Name + " " + item.Menpai + " " + item.Kongfu);
}
Console.WriteLine();
Console.WriteLine("集合操作符:Concat,求两个集合的并集,没有去重");
var selectMaster3 = (
from m1 in masters
where m1.Menpai == "明教"
select m1
).Concat(
from m2 in masters
where m2.Kongfu == "葵花宝典"
select m2
);
foreach (var item in selectMaster3)
{
Console.WriteLine(item.Name + " " + item.Menpai + " " + item.Kongfu);
}
Console.WriteLine();
Console.WriteLine("集合操作符:Except,求两个集合的差集");
var selectMaster4 = (
from m2 in masters
where m2.Kongfu == "葵花宝典"
select m2
).Except(
from m1 in masters
where m1.Menpai == "明教"
select m1);
foreach (var item in selectMaster4)
{
Console.WriteLine(item.Name + " " + item.Menpai + " " + item.Kongfu);
}
Results of the:
Example 9: Aggregate operation of Linq query operation (count, max, min, sum, average, aggregate, longcount)
int[] numbers = { 1, 2, 3, 4, 5, 6, 7 };
int x1 = numbers.Aggregate((n1, n2) => n1 + n2);//28
//int x2 = numbers.Aggregate((n1, n2) => n1 * n2);//5040
int y1 = numbers.Count();//7
//int y2 = numbers.Count(c => c > 3);//4
long z = numbers.LongCount();//7
int sum = numbers.Sum();//28
int max = numbers.Max();//7
int min = numbers.Min();//1
double avg = numbers.Average();//4
Console.WriteLine("Linq聚合操作之Aggregate,用途:对集合元素进行自定义的聚合计算,结果为:" + x1);
Console.WriteLine("Linq聚合操作之Count,用途:求集合元素的个数(32位),结果为:" + y1);
Console.WriteLine("Linq聚合操作之LongCount,用途:求集合元素的个数(64位),结果为:" + z);
Console.WriteLine("Linq聚合操作之Sum,用途:求集合元素的合计值,结果为:" + sum);
Console.WriteLine("Linq聚合操作之Max,用途:求集合元素的最大值,结果为:" + max);
Console.WriteLine("Linq聚合操作之Min,用途:求集合元素的最小值,结果为:" + min);
Console.WriteLine("Linq聚合操作之Average,用途:求集合元素的平均数,结果为:" + avg);
Results of the:
For detailed usage reference of aggregate operation functions: https://www.cnblogs.com/yplong/p/5421830.html