其他分享
首页 > 其他分享> > Linq

Linq

作者:互联网

Linq语句

 

Linq查询表达式以from开头select或group结尾

2021-09-06
         


7


 
 

 



 
 

1

from :指定查询操作的数据源和范围变量
2
join :连接多个用于查询操作的数据源  , 输出相同的值
3
into :提供一个临时标识符,join子句,group子句,或select子句均可以通过该标识符引用查询操作的中间结果
4
let  :引入用来临时保存查询表达式中的字表达式结果的范围变量
5
where:指定筛选元素的逻辑条件
6
orderby:对查询结果进行顺序操作,包括升序和降序
7
select:指定查询结果的类型和表现形式
   

代码

         


143


 
 

 



 
 

1

using System;
2
using System.Linq;
3




4

namespace Linq
5
{
6
    class Program
7
    {
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 Array
18
                     select a;
19




20

            var La2 = from a in Array
21
                      where a < 2
22
                      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 A1
36
                      from a2 in A2
37
                      select a1 + a2;
38




39

            foreach (var item in Lin)
40
            {
41
                Console.Write(item + "  ");
42
            }
43




44

            //给结果值添加名称:i=1 , name = 1
45
            int[] A3 = { 1, 2, 3, 4, 5, 6, 7, 8 };
46
            var Lin2 = from a1 in A3
47
                       select new
48
                       {
49
                           i = a1,
50
                           name = a1.ToString()
51
                       };
52
            foreach (var L in Lin2)
53
            {
54
                Console.WriteLine("Lin2 = " + L);
55
            }
56




57

            //对每个值乘10
58
            int[] A4 = { 1, 2, 3, 4, 5, 6, 7, 8 };
59
            var Lin3 = from a in A4
60
                       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 A5
66
                       where a > 7
67
                       select a;
68




69

            //判断是偶数还是奇数,偶数显示奇数不显示
70
            int[] A6 = { 1, 2, 3, 4, 5, 6, 7, 8 };
71
            var Lin5 = from a in A6
72
                       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 A7
83
                       let n = a % 2
84
                       where n == 0
85
                       select a;
86
            foreach (var item in Lin6)
87
            {
88
                Console.WriteLine(" let语句 =   " + item);
89
            }
90




91

            //降序或升序
92
            var Lin7 = from a in A7
93
                       orderby a descending //ascending
94
                       select a;
95
            foreach (var item in Lin7)
96
            {
97
                Console.WriteLine(" 升序或降序 =    " + item);
98
            }
99




100

            //group  by  分组语句
101
            var Lin8 = from a in A7
102
                       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 into
114




115

            var Lin9 = from a in A7
116
                       group a by a % 2 into Aa
117
                       from InTo in Aa
118
                       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 Array1
131
                        join b in Array2 on a equals b
132
                        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 _Name
3
            from b in _Birth
4
            where a.height < b.Min_Height
5
            orderby a.Id
6
            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 _Name
4
                        from b in _Birth
5
                        where a.Kungfu == b.Kungfu
6
                        orderby a.ko * b.ko descending, a.Age, a.Name
7
                        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


 
 

 



 
 

1

int J = 1;
2
            var Linq4 = from a in _Birth
3
                        where a.ko > 60
4
                        orderby a.ko descending
5
                        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 Linq4
13
                        join b in Linq4_J on a.kungfu equals b.kungfu
14
                        orderby a.ko * b.ko descending
15
                         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

            //Linq
2
            var Linq6 = from a in _Birth
3
                        join b in _Name on a.Kungfu equals b.Kungfu into Groups
4
                        orderby Groups.Count() descending
5
                        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 _Birth
3
            join b in _Name on a.Kungfu equals b.Kungfu
4
            orderby a.Id
5
            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 _Name
3
                        group a by a.Kungfu into b
4
                        orderby b.Key
5
                        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


 
 

 



 
 

1

bool 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 _Name
7
                         join b in _Birth on a.Kungfu equals b.Kungfu
8
                         orderby a.ko * b.ko descending
9
                         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累加 最后结果=21
3
            int arr2 = array.Aggregate(0,(a,b)=>a+b , r=>r*2);         //Aggregate累加 最后结果=42
4
            int arr3 = array.Aggregate(9,(a,b)=>a+b);                 //Aggregate累加 最后结果=30
5
            Console.WriteLine(arr1 +" "+ arr2 + " " + arr3);
      linq模糊查询          


57


 
 

 



 
 

1

1.判断是否为空或者null
2




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 NULL
10




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

       也就是说两个精确字符之间隔了几个字符,在查询的时候就要写几个下划线“_”。
   

标签:Console,Kungfu,ko,Linq,item,WriteLine,var
来源: https://www.cnblogs.com/rdr2/p/15232089.html