Home Ā» Programming Ā» LINQ Ā» Story Details
Printable Version

LINQ Quantifiers Operators

by kimi kalon on Sep 29, 2011

In this story you will see the working of Quantifiers operators in LINQ.
Comments: 0    Views: 244

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.

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                  
              
               LINQ Quantifier operator

  • 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
            
              Quantifier operator in LINQ
 
CONCLUSION 

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

Post a Comment
*
DevExpress PowerBuilder Web Development Windows Development Languages Software Engineering Databases
iPhone Architecture Secutiry UML & Modeling Operating Systems Networking Testing
Graphics Design Project Management Hardware Open Source Games Development Business Intelligence Visual Studio LightSwitch 2011
MonoDevelop Visual Studio 2010 ASP.NET HTML, DHTML XML PHP JavaScript
Silverlight Web Services WCF Windows Forms WPF Windows Services Dynamic Link Libraries
ActiveX COM, DCOM, ATL C# VB.NET C++ F# Java
Pascal SQL Server Oracle DB2 MS-Access Windows Servers Windows
Linux Unix SAP LINQ .NET Framework ADO.NET Reporting
Crystal Reports SQL Server Reporting Services Igenda Reports Active Reports Adobe Fireworks Arrays & Collections Hosting
Future Trends Android Windows Phone Smart Devices Business M&A Investment & Funding
Web Browsers Internet Explorer Firefox Safari Common Entrepreneurs Students
Consulting Wiki Gadgets MobileMe iCloud iOS Social Media
Facebook Twitter LinkedIn Google+ Microsoft Kinect XBox
Wii Playstation DirectX i OS OS X CIO, CTO, CEO Windows 8
Web Design Expression Blend 4 Photoshop CS5 Creative Suite 5.5 Expression Web 4 Expression Studio 4 Creative SuiteĀ® 5.5 Design
Creative Suite 5.5 Web Creative Suite 5.5 Production Startups Funding M&A Laptops Smart Phones
Desktops Cameras & Camcorders Netbooks Tablets Virtualization Microsoft Surface WordPress
Software Products Cloud Computing Current Affairs Technology TV TV
Earnings XAML E-Commerce MonoTouch Mono for Android Deals Electronics
Mobile Phone Laptop Tablet Book Computer Press Releases Reviews
Products Books Companies Windows Azure SQL Azure Interviews Mac
Web Browsers Symbian Windows Forms WPF Windows Services HTML 5 Office 365
SharePoint 2010 Exchange Server Adobe Visual Studio 2012 iPad Flex / Flash Games
Windows 9
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName