前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#LINQ查询子句

C#LINQ查询子句

作者头像
MaybeHC
发布2024-04-23 19:18:03
640
发布2024-04-23 19:18:03
举报
文章被收录于专栏:技术之路技术之路

查询表达式

用查询语法表示的表达式,由一组类似于SQL的语法编写的句子组成 LINQ查询表达式必须以from子句开头,并且必须以select或group子句结束,中间可以添加多个子句

from子句

from子句指定的数据源类型必须为IEnumerable、Ienumerable或者两者的派生类型

代码语言:javascript
复制
int[] nums = {1,7,6,5,8,4,1,2,11,23};
var list = from num in nums select num;

num表示范围变量,表示数据源中的每一个元素,可任意命名 nums表示的是数据源

复合from子句查询

如果数据源(本身是一个序列)的元素还包含子数据源(如序列、列表等),如果要查询子数据源中的元素,则需要使用复合from子句 示例:

代码语言:javascript
复制
  static void Main(string[] args)
        {
            Student obj1 = new Student()
            {
                StuId = 1001,
                StuName = "人员1",
                ScoreList = new List<int>() { 90, 56, 76 }
            };
            Student obj2 = new Student()
            {
                StuId = 1001,
                StuName = "人员2",
                ScoreList = new List<int>() { 40, 78, 46 }
            };
            Student obj3 = new Student()
            {
                StuId = 1001,
                StuName = "人员3",
                ScoreList = new List<int>() { 20, 88, 46 }
            };
            List<Student> stuList = new List<Student>() { obj1, obj2, obj3 };
            var result = from stu in stuList
                         from sc in stu.ScoreList
                         where sc < 40
                         select stu;
            foreach(var item in result)
            {
                Console.WriteLine(item.StuName);
            }
            Console.ReadKey();
        }
在这里插入图片描述
在这里插入图片描述

多个fron子句查询

LINQ查询表达式包含两个或两个以上的独立数据源时,可以使用多个from子句查询所有数据源中的数据 示例:

代码语言:javascript
复制
       static void Main(string[] args)
        {
            Student obj1 = new Student() { StuId = 1001, StuName = "学生1" };
            Student obj2 = new Student() { StuId = 1002, StuName = "学生2" };
            Student obj3 = new Student() { StuId = 1003, StuName = "学生3" };
            Student obj4 = new Student() { StuId = 1004, StuName = "学生4" };
            Student obj5 = new Student() { StuId = 1005, StuName = "学生5" };
            Student obj6 = new Student() { StuId = 1006, StuName = "学生6" };
            List<Student> stuList1 = new List<ConsoleApplication1.Student>() { obj1, obj2, obj3 };
            List<Student> stuList2 = new List<ConsoleApplication1.Student>() { obj4, obj5, obj6 };

            var result = from stu1 in stuList1
                         where stu1.StuId >= 1003
                         from stu2 in stuList2
                         where stu2.StuId >= 1005
                         select new { stu1, stu2 };
            foreach(var item in result)
            {
                Console.WriteLine(item.stu1.StuName+"  "+item.stu1.StuId);
            }
            Console.WriteLine();
            foreach (var item in result)
            {
                Console.WriteLine(item.stu2.StuName + "  " + item.stu2.StuId);
            }
            Console.ReadKey();
        }
在这里插入图片描述
在这里插入图片描述
本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-04-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 查询表达式
    • from子句
      • 复合from子句查询
        • 多个fron子句查询
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
        http://www.vxiaotou.com