Description
Query syntax is written in C# or VB code and which are similar to Query Syntax and is starts with Form clause and followed by a Range variable. User can use standard query Operators to filter, join,group which can written after the Form clause. LINQ have nearly 50 standard Query Operators and Linq Syntax always ends with group or Select clause The query syntax is a shown below.
[csharp]
from <range variable> in <collection>
<filter, joining, grouping, aggregate operators etc.> <lambda expression>
<select or groupBy operator> <formulate the result>
[/csharp]
The syntax below demonstrates the student list from the student collection by using the LINQ query syntax in C# is as shown below.
[csharp]
Student collection
IList<Student> studentList = new List<Student>>() {
new Student() { StudentID = 1, StudentName = "Smith", Age = 13} ,
new Student() { StudentID = 2, StudentName = "Kante", Age = 21 } ,
new Student() { StudentID = 3, StudentName = "Jack", Age = 18 } ,
new Student() { StudentID = 4, StudentName = "Ramk" , Age = 20} ,
new Student() { StudentID = 5, StudentName = "Randy" , Age = 15 }
};
// LINQ Query Syntax to find out teenager students
var teenAgerStudent = from s in studentList
where s.Age > 12 && s.Age < 20
select s;
[/csharp]
The syntax below demonstrates the student list from the student collection by using the LINQ query syntax in VB.Net is as shown below.
[csharp]
// Student collection
Dim studentList = New List(Of Student) From {
New Student() With {.StudentID = 1, .StudentName = "John", .Age = 13},
New Student() With {.StudentID = 2, .StudentName = "Kante", .Age = 21},
New Student() With {.StudentID = 3, .StudentName = "Jack", .Age = 18},
New Student() With {.StudentID = 4, .StudentName = "Ramk", .Age = 20},
New Student() With {.StudentID = 5, .StudentName = "Randy", .Age = 15}
}
// LINQ Query Syntax to find out teenager students
Dim teenAgerStudents As IList(Of Student) = (From s In studentList _
Where s.Age > 12 And s.Age < 20 _
Select s).ToList()
[/csharp]
Description
Method syntax is the extension of the methods in Enumerable or Querable static class and is also known as Fluent Syntax. The syntax below demonstrates the method Syntax is as shown below.
[csharp]
IList<Student> studentList = new List<Student>() {...};
var students = studentList.Where(s => s.age>20).Tolist<Student>();
[/csharp]
In the above syntax extension methods are the where and ToList which are defined as the Enumerable class. If user checked about the extension of the Where method then user will get the where method accepts a predicate delegate as Func<Student, bool>. User can pass the delegate function which accepts the parameter as a Student object and gives a Boolean value. In the below snippet lambda expression worked as a delegate passed in the Where clause.
[csharp]
// Student collection
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
new Student() { StudentID = 2, StudentName = "Kante", Age = 21 } ,
new Student() { StudentID = 3, StudentName = "Jack", Age = 18 } ,
new Student() { StudentID = 4, StudentName = "Ramk" , Age = 20} ,
new Student() { StudentID = 5, StudentName = "Randy" , Age = 15 }
};
// LINQ Method Syntax to find out teenager students
var teenAgerStudents = studentList.Where(s => s.Age > 12 && s.Age < 20)
.ToList<Student>();
[/csharp]
The code below demonstrates the use of the Method syntax with IEnumerable collection is as shown below.
[csharp]
// Student collection
Dim studentList = New List(Of Student) From {
New Student() With {.StudentID = 1, .StudentName = "John", .Age = 13},
New Student() With {.StudentID = 2, .StudentName = "Kante", .Age = 21},
New Student() With {.StudentID = 3, .StudentName = "Jack", .Age = 18},
New Student() With {.StudentID = 4, .StudentName = "Ramk", .Age = 20},
New Student() With {.StudentID = 5, .StudentName = "Randy", .Age = 15}
}
// LINQ Method Syntax to find out teenager students
Dim teenAgerStudents As IList(Of Student) = studentList.Where(Function(s) s.Age > 12 And s.Age < 20)
.ToList()
[/csharp]