Linq query clause

Keywords: SQL

Query expression: an expression expressed in query syntax, which is composed of a group of sentences written in SQL like syntax. Each sub sentence can contain one or more c ා expressions.

Linq query expression contains clauses:

from clause: Specifies the data source and range variable for the query operation

where clause: a logical condition for filtering elements. The return value is a bool type

select clause: specify query result type and representation

Order by clause: sort query results (ascending or descending)

Group clause: group query results

into clause: provides a temporary identifier that can act as a reference to the result of the join/group/select clause

let clause: introduce a range variable to store the result of a subexpression in a query expression

The Linq query expression must include the from clause and start with the from clause. The data source type specified by the from clause must be IEnumerable, IEnumerable < T > or both.

If the data source is a generic type, the compiler can automatically infer the type of the range variable. If the data source is a non generic type, such as ArrayList, the data type of the specified range variable must be displayed.

Composite from clause query:

If the element of a data source (itself a sequence) also contains a sub data source (such as a sequence). If you want to query the elements in the sub data source, you need to use the compound from statement.

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

When the Linq query expression contains two or more independent data sources, multiple from clauses can be used to query the data in all data sources.

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

 

Posted by Scooby Doo on Tue, 03 Dec 2019 06:23:30 -0800