LINQ the Bridge between the world of Objects & the world of Data
page 4 of 6
by Abdulla Hussein AbdelHaq
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 29310/ 66

LINQ to Object

 

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 StringByVal grd As StringByVal 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 _

 

 
 Me.GridView1.DataBind()

 

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

 


View Entire Article

User Comments

Title: G8   
Name: G MURALI
Date: 2010-12-22 7:05:14 PM
Comment:
VERY NICE AND SIMPLY SUPERB; THANKS FOR SUCH A GOOD DESCRIPTION AND INTERSTING TO WORK ON LINQ
Title: Nice One...!   
Name: Sam
Date: 2010-11-27 12:27:17 AM
Comment:
Not Bad...It Helps....
Title: Really veryt very good   
Name: Yashvi
Date: 2009-12-15 6:14:23 AM
Comment:
I had read a lot links to understand LINQ. But i didn't get anything to implement. But yours one is very usefull
Title: Very Useful Links   
Name: Horses
Date: 2009-06-01 12:20:14 PM
Comment:
Very Useful Links are provided here in this article. It is really a nice experience for me to visit your site & read some Quality articles. Thanks.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 1:11:54 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search