SIGN UP MEMBER LOGIN:    
ARTICLE

Quantifiers Operators in LINQ

Posted by Manish Tewatia Articles | LINQ July 19, 2010
In this article you will see the working of Quantifiers operators in LINQ.
 
Reader Level:

Quantifiers operators in LINQ determine a true/ false for the given operator and then return a Boolean value if some or all the elements in a sequence satisfy a condition. There are 2 common Quantifiers operations that can be used in LINQ Any and All.

In this article , you will see some common LINQ where i can use these operators.

I have created a course and student class. Each student belongs to a course. We will query these objects and retrieve information using LINQ through the quantifiers available to us. Here's the sample data:

  
Module Module1
    Sub Main(ByVal args() As String)
        Dim course As New List(Of CourseDetail)()
        course.Add(
New CourseDetail() With {.courseID = 1, .courseName = "BCOM", .Floor = 1})
        course.Add(
New CourseDetail() With {.courseID = 2, .courseName = "BCA", .Floor = 2})
        course.Add(
New CourseDetail() With {.courseID = 3, .courseName = "MCA", .Floor = 3})
        course.Add(
New CourseDetail() With {.courseID = 4, .courseName = "BA", .Floor = 3})
        course.Add(
New CourseDetail() With {.courseID = 5, .courseName = "BSC", .Floor = 3})

        Dim student As New List(Of StudentDetail)()
        student.Add(
New StudentDetail() With {.studentID = 1, .courseID = 1, .studentName = "sapna"})
        student.Add(
New StudentDetail() With {.studentID = 2, .courseID = 4, .studentName = "manish"})
        student.Add(
New StudentDetail() With {.studentID = 3, .courseID = 3, .studentName = "rahul"})
        student.Add(
New StudentDetail() With {.studentID = 4, .courseID = 4, .studentName = "ravi"})
        student.Add(
New StudentDetail() With {.studentID = 5, .courseID = 3, .studentName = "raj"})
        student.Add(
New StudentDetail() With {.studentID = 6, .courseID = 2, .studentName = "shankar"})
        student.Add(
New StudentDetail() With {.studentID = 7, .courseID = 1, .studentName = "ram"})
        student.Add(
New StudentDetail() With {.studentID = 8, .courseID = 1, .studentName = "sam"})
    End Sub

    Friend
Class CourseDetail
        Private privatecourseID As Integer
        Public
Property courseID() As Integer
            Get
                Return
privatecourseID
            End Get
            Set
(ByVal value As Integer)
                privatecourseID = value
            End Set
        End
Property

        Private
privatecourseName As String
        Public
Property courseName() As String
            Get
                Return
privatecourseName
            End Get
            Set
(ByVal value As String)
                privatecourseName = value
            End Set
        End
Property

        Private
privateFloor As Integer
        Public
Property Floor() As Integer
            Get
                Return
privateFloor
            End Get
            Set
(ByVal value As Integer)
                privateFloor = value
            End Set
        End
Property
    End
Class

    Friend
Class StudentDetail
        Private privatestudentID As Integer
        Public
Property studentID() As Integer
            Get
                Return
privatestudentID
            End Get
            Set
(ByVal value As Integer)
                privatestudentID = value
            End Set
        End
Property

        Private
privatecourseID As Integer
        Public
Property courseID() As Integer
            Get
                Return
privatecourseID
            End Get
            Set
(ByVal value As Integer)
                privatecourseID = value
            End Set
        End
Property

        Private
privatestudentName As String
        Public
Property studentName() As String
            Get
                Return
privatestudentName
            End Get
            Set
(ByVal value As String)
                privatestudentName = value
            End Set
        End
Property
    End
Class
  End
Module

  • First we discuss about the Any operator this example shows you to how find the course which don't have any student using Any operator.

        EXAMPLE CODE          

           Dim nostudent = From d In course Where (Not student.Any(Function(e) e.courseID = d.courseID)) Select New With
            _{
Key .dId = d.courseID, Key .dNm =d.courseName}
            Console.WriteLine("Course having no Student")
            For Each studentl In nostudent
                Console.WriteLine("course ID - " & studentl.dId & ", course Name - " & studentl.dNm)
            Next studentl
            Console.ReadLine()

           OUTPUT                  
              
              1.gif

  • Now we the working of All Quantifier operator

        EXAMPLE CODE

            Console.WriteLine("Find if all students have their names starting with 'A'")
            Dim chkName As Boolean = student.All(Function(e) e.studentName.StartsWith("m"))
            Console.WriteLine("Result : " & chkName)
            Console.ReadLine()

           OUTPUT
            
             2.gif
 
CONCLUSION

I hope this article helps you to understand the working of Quantifiers operators.

share this article :
post comment