Linq查询子句

查询表达式:是一种用查询语法表示的表达式,由一组用类似于SQL的语法编写的句子组成,每一个子句可以包含一个或多个c#表达式。

Linq查询表达式包含的子句:

           from子句:指定查询操作的数据源和范围变量

           where子句:筛选元素的逻辑条件,返回值是一个bool类型

           select子句:指定查询结果类型和表现形式

           orderby子句:对查询结果进行排序(升序或降序)

           group子句:对查询结果进行分组

           into子句:提供一个临时标识符,该表示可充当对join/group/select子句结果的引用

           let子句:引入用于存储查询表达式中的子表达式结果的范围变量

           Linq查询表达式必须包括from子句,并且必须以from子句开头,from子句指定的数据源类型必须为IEnumerable,Ienumerable<T>或者两者的派生类型。

           如果数据源是泛型类型,则编译器可以自动推断出范围变量的类型,如果数据源式非泛型类型,如ArrayList,则必须显示的指定范围变量的数据类型。

           复合from子句查询:

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

  Student obj1 = new Student()
            {
                StudentId = 10002,
                StudentName = "jaxk",
                ScoreList = new List<int> { 79, 76, 89 }

            };
            Student obj2 = new Student()
            {
                StudentId = 10003,
                StudentName = "jack",
                ScoreList = new List<int> { 79, 76, 49 }
            };
            List<Student> list = new List<Student>();
            list.Add(obj1);
            list.Add(obj2);
            var result = from stu in list
                         from score in stu.ScoreList
                         where score == 49
                         select stu;
            foreach (var item in result )
            {
                Console.WriteLine(item.StudentName );
            }

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

   Student obj1 = new Student()
            {
                StudentId = 10002,
                StudentName = "jaxk",
                ScoreList = new List<int> { 79, 76, 89 }

            };
            Student obj2 = new Student()
            {
                StudentId = 10003,
                StudentName = "jack",
                ScoreList = new List<int> { 79, 76, 49 }
            };
            Student obj3 = new Student()
            {
                StudentId = 10006,
                StudentName = "lack",
                ScoreList = new List<int> { 79, 76, 49 }
            };
            Student obj4 = new Student()
            {
                StudentId = 10007,
                StudentName = "back",
                ScoreList = new List<int> { 79, 76, 49 }
            };
            List<Student> list = new List<Student>();
            list.Add(obj1);
            list.Add(obj2);
            List<Student> list1 = new List<Student>() { obj3, obj4 };
            var result = from stu in list
                             //from score in stu.ScoreList

                         where stu.StudentName == "jack"
                         from stu1 in list1
                         where stu1.StudentName == "lack"
                         select new { stu, stu1 };
            foreach (var item in result )
            {
                Console.WriteLine(item.stu.StudentId );
                Console.WriteLine(item.stu1 .StudentId );
            }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值