Linq
2021/9/6 6:07:26
本文主要是介绍Linq,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Linq语句
Linq查询表达式以from开头select或group结尾
2021-09-067 1
from :指定查询操作的数据源和范围变量2
join :连接多个用于查询操作的数据源 , 输出相同的值3
into :提供一个临时标识符,join子句,group子句,或select子句均可以通过该标识符引用查询操作的中间结果4
let :引入用来临时保存查询表达式中的字表达式结果的范围变量5
where:指定筛选元素的逻辑条件6
orderby:对查询结果进行顺序操作,包括升序和降序7
select:指定查询结果的类型和表现形式
代码
143 1using System;2
using System.Linq;3 4
namespace Linq5
{6
class Program7
{8
static bool Bl(int a)9
{10
return a % 2 == 0 ? true : false;11
}12 13
static void Main(string[] args)14
{15
int[] Array = { 1, 2, 4, 5, 6, 7, 8 };16 17
var La = from a in Array18
select a;19 20
var La2 = from a in Array21
where a < 222
select a;23 24
foreach (var a in La)25
Console.WriteLine("La = " + a);26 27
foreach (var a in La2)28
Console.WriteLine("La2 = " + a);29 30
Console.WriteLine("L2第一个元素 = " + La2.First());31 32
//两个数组相加,33
int[] A1 = { 1, 2, 3, 4, 5, 6, 7, 8 };34
int[] A2 = { 1, 2, 3, 4, 5, 6, 7, 8 };35
var Lin = from a1 in A136
from a2 in A237
select a1 + a2;38 39
foreach (var item in Lin)40
{41
Console.Write(item + " ");42
}43 44
//给结果值添加名称:i=1 , name = 145
int[] A3 = { 1, 2, 3, 4, 5, 6, 7, 8 };46
var Lin2 = from a1 in A347
select new48
{49
i = a1,50
name = a1.ToString()51
};52
foreach (var L in Lin2)53
{54
Console.WriteLine("Lin2 = " + L);55
}56 57
//对每个值乘1058
int[] A4 = { 1, 2, 3, 4, 5, 6, 7, 8 };59
var Lin3 = from a in A460
select a * 10;61
Console.WriteLine("Lin3 = " + Lin3.First());62 63
//where判断语句64
int[] A5 = { 1, 2, 3, 4, 5, 6, 7, 8 };65
var Lin4 = from a in A566
where a > 767
select a;68 69
//判断是偶数还是奇数,偶数显示奇数不显示70
int[] A6 = { 1, 2, 3, 4, 5, 6, 7, 8 };71
var Lin5 = from a in A672
where Bl(a)73
select a;74
foreach (var i in Lin5)75
{76
Console.WriteLine("判断语句包含函数 = " + i);77 78
}79 80
//let语句, n = a % 2 (=偶数) ,where n == 0 (=偶数)81
int[] A7 = { 1, 2, 3, 4, 5, 6, 7, 8 };82
var Lin6 = from a in A783
let n = a % 284
where n == 085
select a;86
foreach (var item in Lin6)87
{88
Console.WriteLine(" let语句 = " + item);89
}90 91
//降序或升序92
var Lin7 = from a in A793
orderby a descending //ascending94
select a;95
foreach (var item in Lin7)96
{97
Console.WriteLine(" 升序或降序 = " + item);98
}99 100
//group by 分组语句101
var Lin8 = from a in A7102
group a by a % 2;103 104
foreach (var i in Lin8)105
{106
foreach (var j in i)107
{108
Console.WriteLine("group分组语句 = " + j);109 110
}111
}112 113
//group and into114 115
var Lin9 = from a in A7116
group a by a % 2 into Aa117
from InTo in Aa118
select InTo;119 120
foreach (var i in Lin9)121
{122
Console.WriteLine("group and into = " + i);123
}124 125
// join语句,连接两个数组,判断Array1和Array2相同的值126 127
int[] Array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };128
int[] Array2 = { 1, 78, 3, 4, 5, 84, 7, 8, 98 };129 130
var Query = from a in Array1131
join b in Array2 on a equals b132
select a;133 134
foreach (var i in Query)135
{136
Console.WriteLine("Join = "+i);137
}138 139 140
}141
}142
}143
深入_SelectMany(); 存在多个from语句时使用
24 1//常规方法-------------------------------------2
var Linq2 = from a in _Name3
from b in _Birth4
where a.height < b.Min_Height5
orderby a.Id6
select "姓名: " +a.Name+ " 年龄: "+a.Age + " 体重: "+a.Weight + " 身高: "+a.height + " 最低身高: "+b.Min_Height + " 最低体重 "+b.Max_Height + "\n";7 8
Console.WriteLine("不符合条件的有:");9
foreach (var item in Linq2)10
{11
Console.WriteLine(item);12
}13 14
//SelectMany() --------使用方式------------------------------15
var Linq2_J = _Name.SelectMany(B => _Birth, (N, B) => new { n = N, b = B })16
.Where(N => N.n.height < N.b.Min_Height)17
.OrderBy(N => N.n.Id)18
.Select(N=> "姓名: " + N.n.Name + " 年龄: " + N.n.Age + " 体重: " + N.n.Weight + " 身高: " + N.n.height + " 最低身高: " + N.b.Min_Height + " 最低体重 " + N.b.Max_Height + "\n");19 20
Console.WriteLine("不符合条件的有:");21
foreach (var item in Linq2_J)22
{23
Console.WriteLine(item);24
}25 1
//方法1-----------------------------------2
var i = 1;3
var Linq3 = from a in _Name4
from b in _Birth5
where a.Kungfu == b.Kungfu6
orderby a.ko * b.ko descending, a.Age, a.Name7
select "Id: " + a.Id + " 姓名: " + a.Name + " 所练功夫: " + a.Kungfu + " 排位: " + (i++);8
Console.WriteLine("全武林武力值排行榜:");9
foreach (var item in Linq3)10
{11
Console.WriteLine(item);12
}13
//方法2----------------------------------------------------------14
i = 1;15
var Linq3_J = _Name.SelectMany(B => _Birth, (N, B) => new { n = N, b = B })16
.Where(C => C.n.Kungfu == C.b.Kungfu)17
.OrderByDescending(C => C.n.ko * C.b.ko)18
.ThenBy(C => C.n.Name)19
.Select(C=>"ID= "+ C.n.Id+" 姓名: "+C.n.Name+" 所练功夫: "+C.n.Kungfu+"武力值:" + (C.n.ko * C.b.ko) + " 排位: "+(i++));20 21
Console.WriteLine("全武林武力值排行榜:");22
foreach (var item in Linq3_J)23
{24
Console.WriteLine(item);25
}SelectMany()
Join语句
22 1int J = 1;2
var Linq4 = from a in _Birth3
where a.ko > 604
orderby a.ko descending5
select new {id = a.Id,kungfu = a.Kungfu,ko=a.ko,top = (J++) };6
int Ji = 1;7
var Linq4_J = _Name.Where(b => b.ko > 6)8
.OrderByDescending(b => b.ko)9
.Select(b=>new {id=b.Id,name = b.Name,ko = b.ko ,kungfu = b.Kungfu ,top = (Ji++) });10
//连接两个对象进行比较胜负11
int Jii = 1;12
var Linq5 = from a in Linq413
join b in Linq4_J on a.kungfu equals b.kungfu14
orderby a.ko * b.ko descending15
select new { id = b.id, name = b.name, ko = (a.ko * b.ko)16
,kungfu = a.kungfu,Birth = a.top, name_top = b.top, top = Jii++};17
//方法二:使用拓展方法18
int Jiii = 1;19
var Linq5_J = Linq4.Join(Linq4_J, a => a.kungfu, b => b.kungfu, (A, B) =>20
new { id = B.id, name = B.name, ko = (B.ko * A.ko), kungfu = A.kungfu,21
Birth = A.top, name_top = B.top, top = (Jiii++) })22
.OrderByDescending(m=>m.ko);Join
对武林功夫进行排序,此方法是透过count计数排序,判断该武功有多少人使用,从而做到分组
19 1//Linq2
var Linq6 = from a in _Birth3
join b in _Name on a.Kungfu equals b.Kungfu into Groups4
orderby Groups.Count() descending5
select new {Id = a.Id , Kungfu = a.Kungfu , ko = a.ko ,count = Groups.Count() };6
Console.WriteLine("功夫使用人数分组:");7
foreach (var item in Linq6)8
{9
Console.WriteLine("ID: "+item.Id+" 功夫: "+item.Kungfu+" 武力值: "+item.ko +" 此武功使用人数: "+item.count );10
}11 12
//拓展方法13
var Linq6_J = _Birth.GroupJoin(_Name, A => A.Kungfu, B => B.Kungfu, (A, B) => new { A.Id, A.Kungfu, A.ko, count = B.Count() })14
.OrderByDescending(C=>C.count);15
Console.WriteLine("功夫使用人数分组:");16
foreach (var item in Linq6_J)17
{18
Console.WriteLine("ID: "+item.Id+" 功夫: "+item.Kungfu+" 武力值: "+item.ko+" 此功夫使用人数: "+item.count );19
}GroupJoin()
对武林武功进行排序,此方法是透过id安id大小排序做到分组
20 1//Linq语句2
var Linq7 = from a in _Birth3
join b in _Name on a.Kungfu equals b.Kungfu4
orderby a.Id5
select new { Id = a.Id, name = b.Name, Kungfu = a.Kungfu, ko = (a.ko * b.ko) };6 7
Console.WriteLine("joinorGroup分组");8
foreach (var item in Linq7)9
{10
Console.WriteLine("Id= " + item.Id + " 姓名: " + item.name + " 所练功夫: " + item.Kungfu + " 武力值: " + item.ko);11
}12
//拓展方法13
var Linq7_J = _Birth.GroupJoin(_Name, A => A.Kungfu, B => B.Kungfu, (A, B) => new { Id = A.Id, count = B.Count(), Kungfu = A.Kungfu })14
.OrderBy(m=>m.Id);15 16
Console.WriteLine("joinorGroup分组");17
foreach (var item in Linq7_J)18
{19
Console.WriteLine("Id= " + item.Id + " 所练功夫: " + item.Kungfu+" 使用人数: "+item.count);20
}
Group分组武功,linq与Groupby();
19 1//linq语句2
var Linq8 = from a in _Name3
group a by a.Kungfu into b4
orderby b.Key5
select new {kungfu = b.Key,count = b.Count() };6 7
Console.WriteLine("分组个各武功使用数");8
foreach (var item in Linq8)9
{10
Console.WriteLine("功夫名: "+item.kungfu+" 使用人数: "+item.count);11
}12
//拓展方法13
var Lin8_J = _Name.GroupBy(a=>a.Kungfu,(A,B)=>new {kungfu =A,count = B.Count() })14
.OrderBy(m=>m.kungfu);15
Console.WriteLine("分组个各武功使用数");16
foreach (var item in Lin8_J)17
{18
Console.WriteLine("功夫名: " + item.kungfu + " 使用人数: " + item.count);19
}
判断对象里面是否包含指定内容 变量.Any();
2 1bool lbwb = Lin8_J.Any(v=>v.kungfu == "凌波微步");//判断对象是否包含用凌波微步的人2
Console.WriteLine("是否含有使用凌波微步的人:"+lbwb);//返回布尔值判断是否所有对象都一致: 变量.All(); 2 1
bool xlsbz = Lin8_J.All(v=>v.kungfu == "降龙十八掌");2
Console.WriteLine("所有人都使用降龙十八掌:"+xlsbz);判断对象里面是否包含某个对象 3 1
var QiaoFeng = _Name[5];2
bool IsQiaoFeng = _Name.Contains(QiaoFeng);//判断某对象是否存在3
Console.WriteLine("江湖中是否存在乔峰这个人:"+IsQiaoFeng);算出对象总数和页码 1 1 分页 11 1
double Page = 10;//每页数量2
int name_count = _Name.Count();//对象总数3
double count = Math.Ceiling(name_count / Page);//计算总共有多少页4
Console.WriteLine("当前对象总页数:"+count+" 当前对象总数:"+ name_count);5 6
var Linq9 = (from a in _Name7
join b in _Birth on a.Kungfu equals b.Kungfu8
orderby a.ko * b.ko descending9
select new { name = a.Name, Kungfu = b.Kungfu, ko = a.ko * b.ko })10
.Skip(0 * (int)Page) // 0 *Page = 第一页,以此类推11
.Take((int)Page); // 返回指定数量(Page)元素对比两个值返回值相同的集合 9 1
//如果功夫为 降龙十八掌 和武力值为 10 就能返回2
var Linq10 =(from a in _Name where a.Kungfu == "降龙十八掌" select a)3
.Intersect(from b in _Name where b.ko == 10 select b);//Intersect:返回比较值相同的集合4 5
Console.WriteLine("\n比较两个值返回值相同的集合:");6
foreach (var item in Linq10)7
{8
Console.WriteLine(" 姓名 "+item.Name+" 功夫 "+item.Kungfu +" 等级 "+item.ko);9
}累加:Aggregate 5 1
int[] array = { 1,2,3,4,5,6};2
int arr1 = array.Aggregate((a,b)=>a+b); //Aggregate累加 最后结果=213
int arr2 = array.Aggregate(0,(a,b)=>a+b , r=>r*2); //Aggregate累加 最后结果=424
int arr3 = array.Aggregate(9,(a,b)=>a+b); //Aggregate累加 最后结果=305
Console.WriteLine(arr1 +" "+ arr2 + " " + arr3);linq模糊查询 57 1
1.判断是否为空或者null2 3
string.IsNullOrEmpty(des.PlateNum)————————>sql server的PlateNum is null的判断4 5
from des in db.ModelsVehicleRecognition where (!string.IsNullOrEmpty(des.PlateNum)) select new { plateMun = des.PlateNum }6 7
等同于8 9
SELECT PlateNum FROM VehicleRecognition WHERE PlateNum IS NOT NULL10 11
2.普通包含模糊查询12 13
1)以某字符串开头的模糊查询14 15
des.PlateNum.StartsWith("皖A8") ————————>sql server 的 like '皖A8%'16 17
from des in db.ModelsVehicleRecognition where (des.PlateNum.StartsWith("皖A8")) select new { plateMun = des.PlateNum }18 19
等同于20 21
SELECT PlateNum FROM VehicleRecognition WHERE PlateNum like '皖A8%'22 23
2)以某字符串结尾的模糊查询24 25
des.PlateNum.EndsWith("68T") ————————>sql server 的 like '%68T'26 27
from des in db.ModelsVehicleRecognition where (des.PlateNum.EndsWith("68T")) select new { plateMun = des.PlateNum }28 29
等同于30 31
SELECT PlateNum FROM VehicleRecognition WHERE PlateNum like '%68T'32 33
3)包含某字符串的模糊查询34 35
des.PlateNum.Contains("A3") ————————>sql server 的 like '%A3%'36 37
from des in db.ModelsVehicleRecognition where (des.PlateNum.Contains("A3")) select new { plateMun = des.PlateNum }38 39
等同于40 41
SELECT PlateNum FROM VehicleRecognition WHERE PlateNum like '%A3%'42 43
3.精确到字符串对应位数字符的模糊查询(*重点)44 45
SqlFunctions.PatIndex("_a__3%", des.PlateNum) > 0————————>sql server 的 like '_a__3%'46 47
from des in db.ModelsVehicleRecognition where (SqlFunctions.PatIndex("_a__3%", des.PlateNum) > 0) select new { plateMun = des.PlateNum }48 49
等同于50 51
SELECT PlateNum FROM VehicleRecognition WHERE PlateNum like '_a__3%'52 53
说明:'_a__3%' 中的下划线“_”表示一个字符,'_a__3%' 这个字符串查询意思就是第二个字符是a,第五个字符是3的字符串54 55
因为a和3之间有两个下划线“_”所以查询出的结果也要满足a和3之间有两个字符才行,56 57
也就是说两个精确字符之间隔了几个字符,在查询的时候就要写几个下划线“_”。
这篇关于Linq的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-13怎么通过 JavaScript 或其他编程语言来实现监听屏幕高度变化功能?-icode9专业技术文章分享
- 2024-11-12聊聊我们那些年用过的表达式引擎组件
- 2024-11-12让项目数据更有说服力:五款必备数据可视化管理工具推荐
- 2024-11-12人到一定年纪,要学会远离多巴胺
- 2024-11-12解读:精益生产管理的目的是什么?如何操作?
- 2024-11-12Sku预研作业
- 2024-11-12文心一言API密钥:分步申请指南
- 2024-11-12初学者指南:轻松掌握后台交互
- 2024-11-12从零开始学习:封装基础知识详解
- 2024-11-12JSON对象入门教程:轻松掌握基础用法