C# LINQ

2021-07-01 07:05

阅读:674

标签:collect   log   查询   new   false   学习   efault   index   ping   

关键字 说明
from 指定范围变量和数据源
where 根据bool表达式从数据源中筛选数据
select 指定查询结果中的元素所具有的类型或表现形式
group 对查询结果按照键值进行分组(IGrouping)
into 提供一个标识符,它可以充当对join、group或select子句结果的引用
orderby 对查询出的元素进行排序(ascending/descending)
join 按照两个指定匹配条件来Equals连接两个数据源
let 产生一个用于存储查询表达式中的子表达式查询结果的范围变量

1.from子句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace LINQ
{
    /// 
    /// LINQ,重点是感悟from子句中的查询变量的灵活
    /// 
    class Program
    {
        static void Main(string[] args)
        {
            //1单个form子句
            string[] values = { "LINQ学习","LINQ基本语句","from子句","单个from子句"};
            var value = from v in values
                        where v.IndexOf("LINQ") > -1
                        select new { v, v.Length };
            foreach (var n in value)
            {
                Console.WriteLine("{0},{1}",n.v,n.Length );
            }
            Console.ReadKey(false);
            //2使用LINQ查询ArrayList
            ArrayList gList = new ArrayList();
            gList.Add(new GustInfo {  Name="DebugLZQ", Age=26, Tel="88888888"});
            gList.Add(new GustInfo { Name="博客园",Age=6, Tel ="666666"});
            gList.Add(new GustInfo { Name = "M&MSoft", Age =9, Tel = "55555" });

            var query = from GustInfo gust in gList
                        where gust.Age > 9
                        select gust;//范围变量gust制定了数据类型
            foreach (GustInfo g in query)
            {
                Console.WriteLine("{0} 年龄:{1} 电话:{2}",g.Name,g.Age,g.Tel );
            }
            Console.ReadKey(false);
            //3复合from子句
            List gList2 = new List() 
            {
                new GustInfo2{ Name="DebugLZQ",Age=26,TelTable=new List(){"8888888","138******"}},
                new GustInfo2{ Name="博客园",Age=6,TelTable =new List(){"666666","138******"}},
                new GustInfo2{ Name="M&MSoft",Age=9,TelTable=new List(){"55555","138******"}}
            };

            //gust、tel都是查询变量,作用域为当前查询语句!!!
            var query2 = from gust in gList2
                         from tel in gust.TelTable 
                         where tel.IndexOf("5555") > -1
                         select gust;
            foreach (var g in query2)
            {
                Console.WriteLine("{0} 年龄{1}",g.Name,g.Age );
                foreach (var t in g.TelTable)
                {
                    Console.WriteLine("电话:{0}",t);
                }
            }

            Console.ReadKey(false);

            //4多个from子句
            var query3 = from GustInfo gust in gList
                         where gust.Age > 6
                         from GustInfo2 gust2 in gList2
                         where gust2.Age> 9
                         select new { gust, gust2 };//查询结果定制
            foreach (var g in query3)
            {
                Console.WriteLine("{0}  {1}", g.gust.Name, g.gust2.Name);
            }

            Console.ReadKey(false);
        }
    }
}

2.where子句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LINQ_WHERE
{
    /// 
    /// LINQ where子句
    /// 
    class Program
    {
        static void Main(string[] args)
        {
            //1常见的where语句
            List gList = new List() 
            {
                new GustInfo(){ Name="DebugLZQ", Age=26,Tel="88888888"},
                new GustInfo(){ Name="cnblogs",Age=6,Tel="666666"},
                new GustInfo(){ Name="M&MSoft",Age=9,Tel="55555"}
            };

            var query = from gust in gList
                        where (gust.Name.Length > 7 || gust.Name.Substring(0, 1) == "M") && gust.Age > 9
                        select new { gust.Name, gust.Age };
            foreach (var g in query)
            {
                Console.WriteLine("{0},{1}", g.Name, g.Age);
            }

            Console.ReadKey(false);

            //2.在where子句中使用自定义函数
            var query2 = from GustInfo gust in gList
                         where gust.Name.Length > 5
                         && Check(gust.Name)
                         select gust;
            foreach (var g in query2)
            {
                Console.WriteLine("{0},{1},{2}", g.Name, g.Age, g.Tel);
            }
            Console.ReadKey(false);

            //3.动态谓词的筛选
            //定义动态谓词数组,在实际开发中可以动态获得
            string[] names = { "SB","XXX","***","@@@","一些敏感词"};

            var query3 = from GustInfo guest in gList
                         where !names.Contains(guest.Name)
                         select guest;

            foreach (var q in query3)
            {
                Console.WriteLine("{0} 年龄:{1},电话:{2}",q.Name,q.Age,q.Tel );
            }
            Console.ReadKey(false);
        }

        //自定义函数
        static bool Check(string name)
        {
            if (name.Substring(0, 1) == "N")
                return false;
            return true;
        }
    }
}

3.Select子句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LINQ_select
{
    /// 
    /// LINQ select
    /// 在select子句上,可以非常灵活的处理查询到的元素,然后再把结果返回
    /// 
    class MyGustInfo
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List gList = new List() 
            {
                new GustInfo(){ Name="DebugLZQ", Age=25, Tel="88888888"},
                 new GustInfo(){ Name="cnblogs", Age=6, Tel="666666"},
                  new GustInfo(){ Name="M&M", Age=9, Tel="55555"}
            };
            var query = from gust in gList
                        where gust.Age >= 9 && gust.Age = 9 && gust.Age = 9 && gust.Age = 9 && gust.Age 

4.group子句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LINQ_group
{
    /// 
    /// LINQ group子句
    /// 
    class Program
    {
        static void Main(string[] args)
        {
            List gList = new List() 
            {
                new GustInfo(){ Name="DebugLZQ",Age=26,Tel="187********"},
                new GustInfo(){ Name="Sarah",Age=25,Tel="159********"},
                new GustInfo(){ Name="Jerry",Age=35,Tel="135********"},
                new GustInfo(){ Name="M&M",Age=16,Tel="136********"},
                new GustInfo(){ Name="DebugMan",Age=26,Tel="136********"},
                new GustInfo(){ Name="Jerry&Tom",Age=19,Tel="136********"},
            };

            var query = from guest in gList
                        group guest by guest.Name.Substring(0, 1);//分组键key是string类型

            //遍历键值和键值所属元素
            foreach (IGrouping guestGroup in query)
            {
                Console.WriteLine("分组键:{0}",guestGroup.Key );
                foreach (var g in guestGroup)
                {
                    Console.WriteLine("{0} 年龄:{1}  电话:{2}",g.Name,g.Age,g.Tel );
                }
            }
            Console.ReadKey(false);

            Console.WriteLine("-----------------------------------");

            var query2 = from guest in gList
                         group guest by guest.Age > 20;//分组键key是bool类型表达式的结果

            foreach (IGrouping guestGroup in query2)
            {
                Console.WriteLine("年龄是否大于20  分组键:{0}", guestGroup.Key);
                foreach (var g in guestGroup)
                {
                    Console.WriteLine("{0} 年龄:{1}  电话:{2}", g.Name, g.Age, g.Tel);
                }
            }
            Console.ReadKey(false);
        }
    }
}

5.into子句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LINQ_into
{
    /// 
    /// LINQ group
    /// 
    class Program
    {
        static void Main(string[] args)
        {
            List gList = new List() 
            {
                new GustInfo(){ Name="DebugLZQ",Age=26,Tel="187********"},
                new GustInfo(){ Name="Sarah",Age=25,Tel="159********"},
                new GustInfo(){ Name="Jerry",Age=35,Tel="135********"},
                new GustInfo(){ Name="M&M",Age=16,Tel="136********"},
                new GustInfo(){ Name="DebugMan",Age=26,Tel="136********"},
                new GustInfo(){ Name="Jerry&Tom",Age=19,Tel="136********"},
            };

            //1.into用于group子句
            var query = from guest in gList
                        group guest by guest.Name.Substring(0, 1) into grguest
                        orderby grguest.Key descending
                        select grguest;

            var query2 = from guest in gList
                        group guest by guest.Name.Substring(0, 1) into grguest
                        orderby grguest.Key ascending 
                        select grguest;

            //2.select 子句中的into子句
            var query3 = from guest in gList
                         select new { NewName = guest.Name, NewAge = guest.Age } into newguest
                         orderby newguest.NewAge
                         select newguest;

            foreach (var guestGroup in query)
            {
                Console.WriteLine("分组键:{0}",guestGroup.Key );
                foreach (var g in guestGroup)
                {
                    Console.WriteLine("{0} 电话:{1}",g.Name,g.Tel );
                }
            }
            Console.ReadKey(false);

            foreach (var newg in query3)
            {
                Console.WriteLine("{0} 年龄:{1}",newg.NewName,newg.NewAge );
            }

            Console.ReadKey(false);
        }
    }
}

6.orderby子句、thenby子句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LINQ_orderby
{
    class Program
    {
        /// 
        /// LINQ orderby (ascending descending)
        /// 
        /// 
        static void Main(string[] args)
        {
            List gList = new List() 
            {
                new GustInfo(){ Name="DebugLZQ",Age=26,Tel="187********"},
                new GustInfo(){ Name="Sarah",Age=25,Tel="159********"},
                new GustInfo(){ Name="Jerry",Age=35,Tel="135********"},
                new GustInfo(){ Name="M&M",Age=16,Tel="136********"},
                new GustInfo(){ Name="DebugMan",Age=26,Tel="136********"},
                new GustInfo(){ Name="Jerry&Tom",Age=19,Tel="136********"},
            };

            //按照年龄排序
            var query = from guest in gList
                        orderby guest.Age
                        select guest;
            var query1 = from guest in gList
                         orderby guest.Age ascending
                         select guest;
            var query2 = from guest in gList
                         orderby guest.Age descending
                         select guest;
            //按照年龄进行排序,按照名字字数进行次要排序
            var query3 = from guest in gList
                        orderby guest.Age, guest.Name.Length
                        select guest;
            var query4 = from guest in gList
                         orderby guest.Age descending , guest.Name.Length ascending 
                         select guest;
            var query5 = from guest in gList
                         orderby guest.Age, guest.Name.Length,guest.Tel 
                         select guest;

            foreach (var guest in query2)
            {
                Console.WriteLine("{0} 年龄:{1} 电话:{2}",guest.Name,guest.Age,guest.Tel );
            }
            Console.ReadKey(false);
            foreach (var guest in query4)
            {
                Console.WriteLine("{0} 年龄:{1} 电话:{2}", guest.Name, guest.Age, guest.Tel);
            }
            Console.ReadKey(false);
        }
    }
}

7.let子句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LINQ_let
{
    /// 
    /// LINQ let用来存子储表达式的计算结果
    /// 
    class Program
    {
        static void Main(string[] args)
        {
            List gList = new List() 
            {
                new GustInfo(){ Name="DebugLZQ",Age=26,Tel="187********"},
                new GustInfo(){ Name="Sarah",Age=25,Tel="159********"},
                new GustInfo(){ Name="Jerry",Age=35,Tel="135********"},
                new GustInfo(){ Name="M&M",Age=16,Tel="136********"},
                new GustInfo(){ Name="DebugMan",Age=26,Tel="136********"},
                new GustInfo(){ Name="Jerry&Tom",Age=19,Tel="136********"},
            };

            //使用let子句创建范围变量g,并通过g构建查询表达式
            var query = from guest in gList
                        let g = guest.Name.Substring(0, 1)
                        where g == "D" || g == "J"
                        select guest;
            //也可以不使用let,上面的语句等效于下
            var query2 = from guest in gList
                         where guest.Name.Substring(0, 1) == "D" || guest.Name.Substring(0, 1) == "J"
                         select guest;

            foreach (var g in query)
            {
                Console.WriteLine("{0} 年龄:{1} 电话:{2}",g.Name,g.Age,g.Tel );
            }
            Console.ReadKey(false);
            Console.WriteLine("不使用let,等效的语句结果");
            foreach (var g in query2)
            {
                Console.WriteLine("{0} 年龄:{1} 电话:{2}", g.Name, g.Age, g.Tel);
            }
            Console.ReadKey(false);
        }
    }
}

8.join子句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LINQ_join
{
    class Program
    {
        static void Main(string[] args)
        {
            //定义两个数据源
            List gList = new List() 
            {
                new GustInfo(){ Name="DebugLZQ",Age=26,Tel="187********"},
                new GustInfo(){ Name="Sarah",Age=25,Tel="159********"},
                new GustInfo(){ Name="Jerry",Age=35,Tel="135********"},
                new GustInfo(){ Name="M&M",Age=16,Tel="136********"},
                new GustInfo(){ Name="DebugMan",Age=26,Tel="136********"},
                new GustInfo(){ Name="Jerry&Tom",Age=19,Tel="136********"},
            };

            List titleList = new List() 
            {
                new GuestTitle(){Name="DebugLZQ",Title="Soft Engineer"},
                new GuestTitle(){Name="DebugLZQ",Title="Team Leader"},
                new GuestTitle(){Name="Sarah",Title="Test Engineer"},
                new GuestTitle(){Name="Jerry",Title="Head Master"}
            };
            //1.根据姓名进行内连接
            var query = from guest in gList
                        join title in titleList on guest.Name equals title.Name
                        select new { Name=guest.Name ,Title=title.Title,Age=guest.Age };

            foreach (var g in query)
            {
                Console.WriteLine("{0} {1} 年龄:{2}",g.Name,g.Title ,g.Age );
            }
            Console.ReadKey(false);

            //前面的多个from实现相同的作用:与内连接区别在于:这个中间的操作是叉乘获得笛卡尔积
            var query2=from guest in gList 
                       from title in titleList 
                       where guest.Name==title.Name
                       select new { Name = guest.Name, Title = title.Title, Age = guest.Age };

            foreach (var g in query2)
            {
                Console.WriteLine("{0} {1} 年龄:{2}", g.Name, g.Title, g.Age);
            }
            Console.ReadKey(false);

            //2.根据姓名进行分组连接
            //根据名字分组后,得到每个名字下的全部名称
            var query3 = from guest in gList
                         join title in titleList on guest.Name equals title.Name into tgroup
                         select new { Name=guest.Name,Titles=tgroup };

            foreach (var g in query3)
            {
                Console.WriteLine(g.Name);
                foreach (var g2 in g.Titles)
                {
                    Console.WriteLine("    {0}",g2.Title );
                }
            }
            Console.ReadKey(false);
            //3.根据姓名进行左外连接
            //无职务的输出为空缺
            var query4 = from guest in gList
                         join title in titleList on guest.Name equals title.Name into tgroup
                         from subtitle in tgroup.DefaultIfEmpty()
                         select new { Name=guest.Name,Title=subtitle==null?"空缺":subtitle.Title };
            foreach (var g in query4)
            {
                Console.WriteLine("{0}  {1}  ",g.Name ,g.Title );
            }
            Console.ReadKey(false);
            
        }
    }
}

原文链接:https://www.cnblogs.com/DebugLZQ/archive/2012/11/07/2756997.html

C# LINQ

标签:collect   log   查询   new   false   学习   efault   index   ping   

原文地址:https://www.cnblogs.com/MingsonZheng/p/9955689.html


评论


亲,登录后才可以留言!