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 );
            }

猜你喜欢

转载自blog.csdn.net/qq_37589387/article/details/88559971