Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » LINQ » Quantifiers Operators in LINQ

Quantifiers Operators in LINQ

In this article you will see the working of Quantifiers operators in LINQ.

Author Rank :
Page Views : 853
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Dinesh Beniwal
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.