文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

c# Linq查询详解

2024-04-02 19:55

关注

c#提供的ling查询极大的遍历了集合的查询过程,且使用简单方便,非常的有用。

下面将分别用简单的例子说明:ling基本查询、延迟查询属性、类型筛选、复合from字句、多级排序、分组查询、联合查询、合并、分页、聚合操作符、并行linq、取消长时间运行的并行ling查询。

Lambda表达式简介:



              delegate int del(int i);  
              static void Main(string[] args)
              { 
                   del myDelegate = x => x * x;  
                   int j = myDelegate(5); //最后j = 25  
               }

基本查询

语句:


 var query = from r in listStudents where r.score < 60 orderby r.score descending select r;
 //var q2 = listStudents.Where(r => r.score < 60).OrderByDescending(r => r.score).Select(r => r) ;//或使用Lambda表达式

例子:


/// <summary>
        /// 学生结构体
        /// </summary>
        struct Student
        {
            /// <summary>
            /// 姓名
            /// </summary>
            public string name;
            /// <summary>
            /// 年龄
            /// </summary>
            public int age;
            /// <summary>
            /// 班号
            /// </summary>
            public int grade;
            /// <summary>
            /// 分数
            /// </summary>
            public float score;
        }
/// <summary>
        /// linq
        /// 基本查询
        /// </summary>
        static void Linq1()
        {
            #region 构造查询数据
            List<Student> listStudents = new List<Student>();
            Random pRandom = new Random();
            for (int i = 1; i < 50; i++)
            {
                float sc = pRandom.Next(0, 100);
                int age = pRandom.Next(8, 13);
                int gde = pRandom.Next(1, 6);

                string name = "";
                switch (pRandom.Next(0, 6))
                {
                    case 1: name = "周xxx"; break;
                    case 2: name = "李xxx"; break;
                    case 3: name = "孙xxx"; break;
                    case 4: name = "钱xxx"; break;
                    default: name = "赵xxx"; break;
                }

                Student psdt = new Student();
                psdt.name = name;
                psdt.age = age;
                psdt.grade = gde;
                psdt.score = sc;
                listStudents.Add(psdt);
            }
            #endregion
            //从50个学生中选择出不及格的人员名单并按分数降序排列
            var query = from r in listStudents where r.score < 60 orderby r.score descending select r;
            //var q2 = listStudents.Where(r => r.score < 60).OrderByDescending(r => r.score).Select(r => r) ;//或使用Lambda表达式
            //orderby升序即从小到大,orderby r.score descending降序即从大到小

            Console.WriteLine("不及格的人员名单:");
            foreach (Student st in query)
            {
                Console.WriteLine("***************");
                Console.WriteLine("姓名:"+st.name);
                Console.WriteLine("班级:"+st.grade);
                Console.WriteLine("年龄:"+st.age);
                Console.WriteLine("分数:"+st.score);
            }
            Console.ReadKey();
        }

延迟查询属性

linq查询为延迟查询,只需构造一次查询语句,可以多次使用。

例子:


/// <summary>
        /// Linq
        /// 延迟查询
        /// linq查询为延迟查询,只需构造一次查询语句,可以多次使用
        /// </summary>
        static void Linq2()
        {
            #region 构造查询数据
            List<string> lists = new List<string> { "Jack","Pet","Hant","Li","Kkk"};
            
            #endregion
            var query = from r in lists where r.StartsWith("J") select r;
            Console.WriteLine("第一次查询结果:");
            foreach (string st in query)
            {
                Console.WriteLine( st);
            }

            Console.WriteLine("第二次查询结果:");
            lists.Add("Jone");
            lists.Add("Jimi");
            lists.Add("Johu");
            foreach (string st in query)
            {
                Console.WriteLine(st);
            }

            Console.ReadKey();
            
        }

类型筛选

利用OfType方法可以把特定类型数据筛选出来。

例子


/// <summary>
        /// Linq类型筛选-ofType
        /// </summary>
        static void Linq3()
        {
            object[] pList = new object[] { 1,"one",2,"two",3,"three"};
            var query = pList.OfType<string>();
            foreach (var st in query)
            {
                Console.WriteLine(st);
            }
            Console.ReadKey();
            
        }

复合from子句

语句:


 var query = from s in listStudents 
                        from n in s.name 
                        where n == '赵' orderby s.score descending 
                        select s.grade + "班-" + s.name + ",分数" + +s.score;
            

例子:


/// <summary>
        /// linq复合from字句
        /// </summary>
        static void Linq4()
        {
            #region 构造查询数据
            List<Student> listStudents = new List<Student>();
            Random pRandom = new Random();
            for (int i = 1; i < 50; i++)
            {
                float sc = pRandom.Next(0, 100);
                int age = pRandom.Next(8, 13);
                int gde = pRandom.Next(1, 6);

                string name = "";
                switch (pRandom.Next(0, 6))
                {
                    case 1: name = "周xxx"; break;
                    case 2: name = "李xxx"; break;
                    case 3: name = "孙xxx"; break;
                    case 4: name = "钱xxx"; break;
                    default: name = "赵xxx"; break;
                }

                Student psdt = new Student();
                psdt.name = name;
                psdt.age = age;
                psdt.grade = gde;
                psdt.score = sc;
                listStudents.Add(psdt);
            }
            #endregion
            var query = from s in listStudents 
                        from n in s.name 
                        where n == '赵' orderby s.score descending 
                        select s.grade + "班-" + s.name + ",分数" + +s.score;
            
            foreach (var t in query)
            {
                Console.WriteLine(t);
            }
            Console.ReadKey();
        }

多级排序

语句:


 //参数越靠前,优先级越高
            //先按score排序,当分数相同时再按grade排序...依次类推
            var query = from s in listStudents orderby s.score, s.grade, s.age, s.name select s;

例子:


/// <summary>
        /// 多级排序
        /// </summary>
        static void Linq5()
        {
            #region 构造查询数据
            List<Student> listStudents = new List<Student>();
            Random pRandom = new Random();
            for (int i = 1; i < 50; i++)
            {
                float sc = pRandom.Next(0, 100);
                int age = pRandom.Next(8, 13);
                int gde = pRandom.Next(1, 6);

                string name = "";
                switch (pRandom.Next(0, 6))
                {
                    case 1: name = "周xxx"; break;
                    case 2: name = "李xxx"; break;
                    case 3: name = "孙xxx"; break;
                    case 4: name = "钱xxx"; break;
                    default: name = "赵xxx"; break;
                }

                Student psdt = new Student();
                psdt.name = name;
                psdt.age = age;
                psdt.grade = gde;
                psdt.score = sc;
                listStudents.Add(psdt);
            }
            #endregion
            //参数越靠前,优先级越高
            //先按score排序,当分数相同时再按grade排序...依次类推
            var query = from s in listStudents orderby s.score, s.grade, s.age, s.name select s;
            foreach (Student st in query)
            {
                Console.WriteLine("***************");
                Console.WriteLine("姓名:" + st.name);
                Console.WriteLine("班级:" + st.grade);
                Console.WriteLine("年龄:" + st.age);
                Console.WriteLine("分数:" + st.score);
            }
            Console.ReadKey();
        }

分组

语句:


//按国家分组,并选出大于2的组,形成新的集合
            var query = from r in listChampion
                        group r by r.country into g
                        orderby g.Count() descending, g.Key
                        where g.Count() >= 2
                        select
                            new { country = g.Key, count = g.Count() };

例子:


/// <summary>
        /// 分组
        /// </summary>
        static void Linq6()
        {
            List<Champions> listChampion = new List<Champions>();
            listChampion.Add(new Champions() { name = "张**", country = "中国" });
            listChampion.Add(new Champions() { name = "赵**", country = "中国" });
            listChampion.Add(new Champions() { name = "李**", country = "中国" });
             listChampion.Add(new Champions() { name = "李**", country = "中国" });
            listChampion.Add(new Champions() { name = "Peter", country = "美国" });
            listChampion.Add(new Champions() { name = "Hune", country = "美国" });
              listChampion.Add(new Champions() { name = "Hune", country = "美国" });
            listChampion.Add(new Champions() { name = "Jack", country = "俄罗斯" });
              listChampion.Add(new Champions() { name = "Jack", country = "俄罗斯" });
            listChampion.Add(new Champions() { name = "Jimi", country = "英国" });
            //按国家分组,并选出大于2的组,形成新的集合
            var query = from r in listChampion
                        group r by r.country into g
                        orderby g.Count() descending, g.Key
                        where g.Count() >= 2
                        select
                            new { country = g.Key, count = g.Count() };
            foreach (var o in query)
            {
                Console.WriteLine("国家:{0},冠军数:{1}个", o.country, o.count);
            }
            Console.ReadKey();
        }

联合查询-join

语句:


//查询出集合qSt中year等于集合qSc中year的元素并形成新的集合
            var qjoin = from r in qSt 
                        join c in qSc 
                        on r.year equals c.year 
                        select new 
                        {
                            Year = r.year, 
                            stName = r.name,
                            scName = c.name 
                        };

例子:


/// <summary>
        /// 联合查询-join
        /// </summary>
        static void Linq7()
        {
            List<s> pSt = new List<s>();
            pSt.Add(new s() {year=1999,name="xxx" });
            pSt.Add(new s() { year = 2000, name = "xxx" });
            pSt.Add(new s() { year = 2001, name = "xxx" });
            pSt.Add(new s() { year = 2010, name = "xxx" });
            List<school> pSc = new List<school>();
            pSc.Add(new school() { year = 1999, name = "***" });
             pSc.Add(new school() { year = 2001, name = "***" });
            pSc.Add(new school() { year = 2002, name = "***" });
            pSc.Add(new school() { year = 2010, name = "***" });
            pSc.Add(new school() { year = 2012, name = "***" });

            var qSt = from r in pSt where r.year >= 2000 select r;
            var qSc = from r in pSc where r.year >= 2000 select r;

            //查询出集合qSt中year等于集合qSc中year的元素并形成新的集合
            var qjoin = from r in qSt 
                        join c in qSc 
                        on r.year equals c.year 
                        select new 
                        {
                            Year = r.year, 
                            stName = r.name,
                            scName = c.name 
                        };

            foreach (var ite in qjoin)
            {
                Console.WriteLine(ite.Year + " " + ite.scName + " " + ite.stName);
                Console.WriteLine("");
            }
            Console.ReadKey();
        }

合并-zip()

.Net4.0新增,可对两个相关的序列进行合并。

语句:


  
            var qZip = qSc.Zip(qSt, (first, second) =>string.Format("Year:{0},Name:{1}.", first.Year,second.Name));//返回值qZip为字符串集合

例子:


/// <summary>
        /// 合并-zip()-.Net4.0新增,可对两个相关的序列进行合并
        /// </summary>
        static void Linq8()
        {
            List<s> pSt = new List<s>();
            pSt.Add(new s() { year = 1999, name = "一xxx" });
            pSt.Add(new s() { year = 2000, name = "二xxx" });
            pSt.Add(new s() { year = 2001, name = "三xxx" });
            pSt.Add(new s() { year = 2010, name = "四xxx" });
            pSt.Add(new s() { year = 2010, name = "五xxx" });
            List<school> pSc = new List<school>();
            pSc.Add(new school() { year = 1999, name = "1***" });
            pSc.Add(new school() { year = 2012, name = "2***" });
            pSc.Add(new school() { year = 2012, name = "3***" });
            pSc.Add(new school() { year = 2012, name = "4***" });
            pSc.Add(new school() { year = 2012, name = "5***" });

            var qSt = from r in pSt where r.year >= 2000 orderby r.year select new { Year = r.year, Name = r.name };
            var qSc = from r in pSc where r.year >= 2000 orderby r.year select new { Year = r.year, Name = r.name };
            
            var qZip = qSc.Zip(qSt, (first, second) =>string.Format("Year:{0},Name:{1}.", first.Year,second.Name));//返回值qZip为字符串集合
           
            foreach (var ite in qZip)
            {
                Console.WriteLine(ite);
            }
            Console.ReadKey();

        }

分区(分页)

通过Take()和Skip()实现只显示部分查询结果。

语句:


 //通过skip跳过指定数量的元素,再通过take提取固定长度元素,可实现分页
                var qr = (from r in listStudents orderby r.score descending select r).Skip(i * pageSize).Take(5);
                //var qr2 = listStudents.OrderByDescending(r => r.score).Select(r => r).Skip(i * pageSize).Take(5);//或

例子


/// <summary>
        /// 分区(分页)-通过Take()和Skip()实现只显示部分查询结果
        /// </summary>
        static void Linq9()
        {
            #region 构造查询数据
            List<Student> listStudents = new List<Student>();
            Random pRandom = new Random();
            for (int i = 1; i < 50; i++)
            {
                float sc = pRandom.Next(0, 100);
                int age = pRandom.Next(8, 13);
                int gde = pRandom.Next(1, 6);

                string name = "";
                switch (pRandom.Next(0, 6))
                {
                    case 1: name = "周xxx"; break;
                    case 2: name = "李xxx"; break;
                    case 3: name = "孙xxx"; break;
                    case 4: name = "钱xxx"; break;
                    default: name = "赵xxx"; break;
                }

                Student psdt = new Student();
                psdt.name = name;
                psdt.age = age;
                psdt.grade = gde;
                psdt.score = sc;
                listStudents.Add(psdt);
            }
            #endregion

            int pageSize = 5;
            int numofPage = (int)Math.Ceiling(listStudents.Count /(double)pageSize);

            for (int i = 0; i < numofPage; i++)
            {
                Console.WriteLine("第{0}页", i);

                //通过skip跳过指定数量的元素,再通过take提取固定长度元素,可实现分页
                var qr = (from r in listStudents orderby r.score descending select r).Skip(i * pageSize).Take(5);
                //var qr2 = listStudents.OrderByDescending(r => r.score).Select(r => r).Skip(i * pageSize).Take(5);//或
                foreach (var ite in qr)
                {
                    Console.WriteLine(ite.name);
                    Console.WriteLine(ite.score);
                }
                Console.WriteLine("");
            }
            Console.ReadKey();
        }

并行linq

并行查询可以分解查询的工作,使其分布在多个线程上。当pc拥有多个cpu时,可以看到并行查询带来的改进效果。并行LINQ适用于大型的集合查询,并拥有一定的优势。使用

System.Collections.Concurrent.Partitioner.Create
可以手动创建分区器。
语法:


var sum2 = (from x2 in data.AsParallel() where x2 > 20 select x2).Sum();//并行查询

//var sum3 = data.AsParallel().Where(x3 => x3 > 20).Sum();//或并行查询(Lambda表达式)

例子:


/// <summary>
        /// 并行linq
        /// </summary>
        static void Linq11()
        {
            Console.WriteLine("开始构造大数组...");
            //构造大数组
            const int count = 100000000;
            var data = new int[count];
            var r = new Random();
            for (int i = 0; i < count; i++)
            {
                data[i] = r.Next(40);
            }
            Console.WriteLine("开始计算...");
            var st = System.DateTime.Now;
            var sum = (from x in data where x > 20 select x).Sum();//常规linq-耗时1.8641s
            var st2 = System.DateTime.Now;
            var sum2 = (from x2 in data.AsParallel() where x2 > 20 select x2).Sum();//并行查询-耗时0.6620s

            //var sum3 = data.AsParallel().Where(x3 => x3 > 20).Sum();//或并行查询----x3 => x3 > 20(Lambda表达式)
            var st3 = System.DateTime.Now;

            
            var sum4 = (from c in System.Collections.Concurrent.Partitioner.Create(data, true).AsParallel() where c > 20 select c).Sum();

            var dt1 = st2 - st;
            var dt2 = st3 - st2;
            Console.WriteLine("常规linq耗时:{0}s",dt1.TotalSeconds.ToString());
            Console.WriteLine("并行linq耗时:{0}s", dt2.TotalSeconds.ToString());
            Console.ReadKey();
        }

输出结果:

可以粗鲁的认为并行linq对于大集合的查询是优势比较明显的。

取消长时间运行的并行linq查询

对于并行ling而言,可以设置可以利用System.Threading.CancellationTokenSource设置取消操作。

语法:


 //通过.WithCancellation(cts.Token)设置
                    var sum = (from x in data.AsParallel().WithCancellation(cts.Token) where x < 20 select x).Average();

例子:


/// <summary>
        /// 取消长时间运行的并行linq查询
        /// </summary>
        static void Linq12()
        {
            //构造大数组
            const int count = 100000000;
            var data = new int[count];
            var r = new Random();
            for (int i = 0; i < count; i++)
            {
                data[i] = r.Next(40);
            }

            var cts = new System.Threading.CancellationTokenSource();

            new System.Threading.Thread(() =>
            {
                try
                {
                    //通过.WithCancellation(cts.Token)设置
                    var sum = (from x in data.AsParallel().WithCancellation(cts.Token) where x < 20 select x).Average();
                    Console.WriteLine("sum:{0}", sum);
                }
                catch(OperationCanceledException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }).Start();

            Console.WriteLine("计算开始...");
            Console.WriteLine("取消请按n!");
            var rk = Console.Read();
            if(rk=='n'||rk=='N')
            {
                cts.Cancel();
            }

        }

至此,对于LINQ已经有了一个基本的了解。

以上就是c# Linq查询详解的详细内容,更多关于c# Linq查询的资料请关注编程网其它相关文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯