To get start working with LINQ, we need to create a class,
let's name it "Student" class that contains student name, student grade
and student class.
All next samples will be applied on this class.
Listing 3: Student Class
Public Class Student
Private _stName As String = String.Empty
Private _stGrade As String = String.Empty
Private _stClass As String = String.Empty
Public Sub New(ByVal nme As String, ByVal grd As String, ByVal cls As String)
_stName = nme
_stGrade = grd
_stClass = cls
End Sub
Public Property StudentName() As String
Get
Return _stName
End Get
Set(ByVal value As String)
_stName = value
End Set
End Property
Public Property StudentGrade() As String
Get
Return _stGrade
End Get
Set(ByVal value As String)
_stGrade = value
End Set
End Property
Public Property StudentClass() As String
Get
Return _stClass
End Get
Set(ByVal value As String)
_stClass = value
End Set
End Property
End Class
Now create an object from that class and add some new
elements.
Listing 4.
Dim objStudent As New List(Of Student)
objStudent.Add(New Student("Abdulla", "92", "10"))
objStudent.Add(New Student("Ali", "98", "8"))
objStudent.Add(New Student("Rula", "95", "10"))
objStudent.Add(New Student("Oday", "95", "9"))
objStudent.Add(New Student("Belal", "79", "8"))
objStudent.Add(New Student("Sara", "80", "8"))
Ok, if I ask you to list only student who is name contain
"a" character, and his grade more than 90, and he should be in 10
class, Also I want the list to be displayed order by student name descending.
Finally I want student name to be shown in lowercase mode.
Let's solve this using the regular code, look at Listing 5
Listing 5 Regular Code
Dim obj2 As New List(Of Student)
Dim strName, strGrade, strClass As String
For i As Integer = 0 To objStudent.Count - 1
If objStudent.Item(i).StudentName.Contains("a") Then
If objStudent.Item(i).StudentGrade > 90 Then
If objStudent.Item(i).StudentClass = 10 Then
strName = objStudent.Item(i).StudentName.ToLower
strGrade = objStudent.Item(i).StudentGrade
strClass = objStudent.Item(i).StudentClass
obj2.Add(New Student(strName, strGrade, strClass))
End If
End If
End If
Next
obj2.Reverse()
Me.GridView1.DataSource = obj2
Me.GridView1.DataBind()
Even it was a very simple question, but we have used for
loop, 3 nested if condition and we declared new generic list of Student class
to collect the filtered students and new three variables too. In addition we used
the reverse method to get the new student list to be order in descending way.
Now let's see how LINQ could solve that question only in one
line of code using query statements!
Take another Look to the list of student, and make it as you
are looking to a table in SQL server.
Listing 6: LINQ code
Me.GridView1.DataSource = From st in objStudent _
Where st.StudentName.Contains("a") _
AND st.StudentGrade > 90 AND st.StudentClass = 10 _
Order By st.StudentName Descending
Select st.StudentName.Tolower, st.StudentGrade, st.StudentClass _
SQL statement within VB code, amazing ! Not yet, LINQ return
results of type IEnumerable(Of T) (T here will be Student Class), which means
that you can bind it directly to any databound control, also LINQ result is a
safe type object! I really appreciate that.
What about grouping, can I do that with LINQ? Yes you can,
look at Listing 7, LINQ groups our student list to be grouped by student class.
Listing 7: LINQ grouping
Me.GridView1.DataSource = from st in objStudent _
group st by st.StudentClass
Me.GridView1.DataBind()
If you note that in Listing 7, I did not use
"select" statement! If you did not specify certain property in select
statement, then LINQ will return by default all properties in that object.
We can also skip out some rows and take a certain rows too.
Listing 8, LINQ will skip the first 3 rows that returned from the query, and it
will take only 10 rows.
Listing 8: LINQ Skip, Take
Me.GridView1.DataSource = (From st in objStudent _
Where st.StudentName.Contains("a") _
AND st.StudentGrade > 90 AND st.StudentClass = 10 _
Order By st.StudentName Descending
Select st.StudentName.Tolower, st.StudentGrade, st.StudentClass).Skip(3).Take(10);
Me.GridView1.DataBind