博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linq入门演练---(1)基本用法-分组,排序,内连接
阅读量:6327 次
发布时间:2019-06-22

本文共 6608 字,大约阅读时间需要 22 分钟。

这一节大家共同学习下LINQ的基本用法,主要包括LINQ的分组,排序,和内外连接。

1.分组

   基本语法:  group element by key

                      element 表示查询结果返回的元素,key表示分组条件。group子句返回的类型为IGrouping<TKey,TElement>的查询结果。

                      IGrouping<TKey,TElement>可以看成一个hashtable内部嵌套一个list列表的数据结果. 其中TElement就是一个list

我们看下面的一个事例,下面这个实例演示根据性别对用户进行分组

代码

 

[csharp]
  1. publicclass User 
  2.     { 
  3.         public User(string userName,string sex,int age) 
  4.         { 
  5.             UserName = userName; 
  6.             Sex = sex; 
  7.             Age = age; 
  8.         } 
  9.  
  10.         publicstring UserName 
  11.         { 
  12.             get
  13.             set
  14.         } 
  15.  
  16.         publicstring Sex 
  17.         { 
  18.             get
  19.             set
  20.         } 
  21.  
  22.         publicint Age 
  23.         { 
  24.             get
  25.             set
  26.         } 
  27.     } 
public class User    {        public User(string userName,string sex,int age)        {            UserName = userName;            Sex = sex;            Age = age;        }        public string UserName        {            get;            set;        }        public string Sex        {            get;            set;        }        public int Age        {            get;            set;        }    }

 

 

[csharp]
  1. privatevoid button1_Click(object sender, EventArgs e) 
  2.         { 
  3.             User[] users = {  
  4.                            new User("刘德华","男",40), 
  5.                            new User("张学友","男",30), 
  6.                            new User("张曼玉","女",35), 
  7.                            new User("杨幂","女",53) 
  8.                            }; 
  9.  
  10.             var query1 = 
  11.                   from var1 in users 
  12.                   group var1 by var1.Sex; 
  13.  
  14.             string str = ""
  15.             foreach (var item in query1) 
  16.             { 
  17.                 str += item.Key + "\r\n"
  18.  
  19.                 foreach (var user in item) 
  20.                 { 
  21.                     str += user.UserName + "--" + user.Sex + "--" + user.Age.ToString() + "\r\n"
  22.                 } 
  23.               
  24.             } 
  25.             textBox1.Text = str; 
  26.  
  27.         } 
private void button1_Click(object sender, EventArgs e)        {            User[] users = {                            new User("刘德华","男",40),                           new User("张学友","男",30),                           new User("张曼玉","女",35),                           new User("杨幂","女",53)                           };            var query1 =                  from var1 in users                  group var1 by var1.Sex;            string str = "";            foreach (var item in query1)            {                str += item.Key + "\r\n";                foreach (var user in item)                {                    str += user.UserName + "--" + user.Sex + "--" + user.Age.ToString() + "\r\n";                }                         }            textBox1.Text = str;        }

 

 

效果图

以上是简单的分组,如果需要对分组结果进行排序,需要把分组结果保存到一个临时变量中

语法: group element by key into  temp

   以下是按年龄从小到大的排序:

  代码

[csharp]
  1. User[] users = {  
  2.                           new User("刘德华","男",40), 
  3.                           new User("张学友","男",30), 
  4.                           new User("张曼玉","女",35), 
  5.                           new User("杨幂","女",53) 
  6.                           }; 
  7.  
  8.            var query1 = 
  9.                  from var1 in users 
  10.                  group var1 by var1.Age into temp 
  11.                  orderby temp.Key descending 
  12.                  select temp; 
  13.  
  14.            string str = ""
  15.            foreach (var item in query1) 
  16.            { 
  17.                str += item.Key + "\r\n"
  18.  
  19.                foreach (var user in item) 
  20.                { 
  21.                    str += user.UserName + "--" + user.Sex + "--" + user.Age.ToString() + "\r\n"
  22.                } 
  23.              
  24.            } 
  25.            textBox1.Text = str; 
User[] users = {                            new User("刘德华","男",40),                           new User("张学友","男",30),                           new User("张曼玉","女",35),                           new User("杨幂","女",53)                           };            var query1 =                  from var1 in users                  group var1 by var1.Age into temp                  orderby temp.Key descending                  select temp;            string str = "";            foreach (var item in query1)            {                str += item.Key + "\r\n";                foreach (var user in item)                {                    str += user.UserName + "--" + user.Sex + "--" + user.Age.ToString() + "\r\n";                }                         }            textBox1.Text = str;

 

 

   效果图

 

 

2.排序

    在linq中排序使用orderby

    语法 orderby elment  descending||ascending

      ascending表示升序,descending表示降序

    下面这个DEMO演示按年龄从大到小排序

    代码:

    

[csharp]
  1. User[] users = {  
  2.                            new User("刘德华","男",40), 
  3.                            new User("张学友","男",30), 
  4.                            new User("张曼玉","女",35), 
  5.                            new User("杨幂","女",53) 
  6.                            }; 
  7.  
  8.             var query1 = 
  9.                   from var1 in users 
  10.                   orderby var1.Age descending 
  11.                   select var1; 
  12.  
  13.             string str = ""
  14.             foreach (var item in query1) 
  15.             { 
  16.                 str += item.UserName + "--" + item.Age + "\r\n"
  17.             } 
  18.             textBox1.Text = str; 
User[] users = {                            new User("刘德华","男",40),                           new User("张学友","男",30),                           new User("张曼玉","女",35),                           new User("杨幂","女",53)                           };            var query1 =                  from var1 in users                  orderby var1.Age descending                  select var1;            string str = "";            foreach (var item in query1)            {                str += item.UserName + "--" + item.Age + "\r\n";            }            textBox1.Text = str;

  效果图

  

 

3.内连接

    内连接查询就是根据一定的条件查询两个数据源中都存在的元素

   语法: join elment  in datasource on re1 equals re2

   以下代码查找两个数组中都存在的元素。

  代码:

[csharp]
  1. privatevoid button3_Click(object sender, EventArgs e) 
  2.         { 
  3.             //查询两个数组中都存在的元素 
  4.             int[] arrOne = { 10,20,30,6,8}; 
  5.             int[] arrTwo = { 10,4,6,8,90,3}; 
  6.  
  7.             var query = from re1 in arrOne 
  8.                         join re2 in arrTwo on re1 equals re2 
  9.                         select new { re1 = re1, re2 = re2 }; 
  10.  
  11.             foreach (var item in query) 
  12.             { 
  13.                 Console.WriteLine(item); 
  14.             } 
  15.  
  16.         } 
private void button3_Click(object sender, EventArgs e)        {            //查询两个数组中都存在的元素            int[] arrOne = { 10,20,30,6,8};            int[] arrTwo = { 10,4,6,8,90,3};            var query = from re1 in arrOne                        join re2 in arrTwo on re1 equals re2                        select new { re1 = re1, re2 = re2 };            foreach (var item in query)            {                Console.WriteLine(item);            }        }

 

  查询结果:

[csharp]
  1. { re1 = 10, re2 = 10 } 
  2. { re1 = 6, re2 = 6 } 
  3. { re1 = 8, re2 = 8 } 
{ re1 = 10, re2 = 10 }{ re1 = 6, re2 = 6 }{ re1 = 8, re2 = 8 }

 

4.左连接

左连接和SQL中的左连接类似,返回第一个结果集中的所有元素。

下面的代码演示使用左连接,左连接要用到defaultempty()方法,此方法从列表中获取指定元素,如果列表为空,返回默认值

代码

[csharp]
  1. privatevoid button4_Click(object sender, EventArgs e) 
  2.       { 
  3.           //左连接 
  4.           int[] arrOne = { 10, 20, 30, 6, 8 }; 
  5.           int[] arrTwo = { 10, 4, 6, 8, 90, 3 }; 
  6.  
  7.           var query = from re1 in arrOne 
  8.                       join re2 in arrTwo on re1 equals re2 into temgrp 
  9.                       from grp in temgrp.DefaultIfEmpty() 
  10.                       select new { re1 = re1, re2 = grp }; 
  11.  
  12.           foreach (var item in query) 
  13.           { 
  14.               Console.WriteLine(item); 
  15.           } 
  16.       } 
private void button4_Click(object sender, EventArgs e)        {            //左连接            int[] arrOne = { 10, 20, 30, 6, 8 };            int[] arrTwo = { 10, 4, 6, 8, 90, 3 };            var query = from re1 in arrOne                        join re2 in arrTwo on re1 equals re2 into temgrp                        from grp in temgrp.DefaultIfEmpty()                        select new { re1 = re1, re2 = grp };            foreach (var item in query)            {                Console.WriteLine(item);            }        }

 

 

查询结果

[csharp]
  1. { re1 = 10, re2 = 10 } 
  2. { re1 = 20, re2 = 0 } 
  3. { re1 = 30, re2 = 0 } 
  4. { re1 = 6, re2 = 6 } 
  5. { re1 = 8, re2 = 8 } 

转载于:https://www.cnblogs.com/itjeff/p/3359540.html

你可能感兴趣的文章
Sybase常用函数
查看>>
RMAN-format变量及configuration配置项
查看>>
Properties中的主要方法
查看>>
LeetCode44.通配符匹配
查看>>
Win7 IIS 局域网中无法访问网页
查看>>
idea中maven web项目设置的相关问题
查看>>
【java】静态方法声明与调用习题
查看>>
C#中数组、ArrayList和List三者的区别
查看>>
项目总结(2010-8-8)
查看>>
MYSQL-innodb性能优化几个点
查看>>
nstimer,需要注册到 runloop里面去的,
查看>>
C# Socket系列3 Socket服务端开发
查看>>
Spring中IOC和AOP的详细解释
查看>>
SQL Server 表分区(partitioned table/Data Partitioning)
查看>>
linux环境下学习使用pro*c/c++工具
查看>>
Unity游戏开发之“屏幕截图”
查看>>
GDB Layout
查看>>
剑指Offer——面试题27:二叉搜索树与双向链表
查看>>
【HDOJ】2585 Hotel
查看>>
个人作业 软件案例分析
查看>>