Your first query in LINQ
Microsoft added lot of new technology features in .Net 3.0 / 3.5. LINQ is newly added feature of .Net. From the official LINQ home page, The LINQ Project is a codename for a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. Few days back I come into a situation where I need to reduce the hit count of a particular method*, which returns a collection class, based on an argument to the method. initially I played around select case,
if – else etc, but then I come to know that the parameters can be changed. So started with LINQ.
'Creating the collection
Dim oPersonCollection As New PersonCollection()
'Adding items to the collection
oPersonCollection.Add(New Person With {.Name = "Person1", .Age = 20})
oPersonCollection.Add(New Person With {.Name = "Person2", .Age = 26})
oPersonCollection.Add(New Person With {.Name = "Person3", .Age = 23})
oPersonCollection.Add(New Person With {.Name = "Person5", .Age = 22})
oPersonCollection.Add(New Person With {.Name = "Person7", .Age = 29})
oPersonCollection.Add(New Person With {.Name = "Person4", .Age = 25})
oPersonCollection.Add(New Person With {.Name = "Person9", .Age = 20})
'Querying the collection to get all Persons from the
'collection with Age >= 20 and Age = 20 And oPerson.Age <= 23
'Binding the list to Dropdown
Me.ComboBox1.DataSource = Query.ToList()
And the implementation of PersonCollection and Person class.
Public Class PersonCollection Inherits List(Of Person) 'No Implementation Reqd. You can use a simple List or Collection. End Class 'Simple Person class, with Name and Age Properties Public Class Person Private m_Name As String Public Property Name() As String Get Return m_Name End Get Set(ByVal value As String) m_Name = value End Set End Property Private m_Age As Integer Public Property Age() As Integer Get Return m_Age End Get Set(ByVal value As Integer) m_Age = value End Set End Property 'To display the value, by directly binding to the listbox. Public Overrides Function ToString() As String Return m_Name & " with an age of " & m_Age End Function End Class
Also VB.Net contains support for XML too.
Links : Official LINQ Project Page
* For last few days I am working on a WPF Project, in the Role of “Performance optimizer”
No Responses to “Your first query in LINQ”
RSS feed for comments on this post. TrackBack URL
I got to see ur blog.
Its nice… but I have a query
the one u have specified as “My” in the below line
“My.Computer.FileSystem.SpecialDirectories.Desktop”
whats that?
Comment by Vinoth — December 18, 2008 @ 5:48 am
Thanks Vinoth. Thanks for your comments. I thought you have some issue with Linq. But I think it is from the latest post. “My.Computer.FileSystem.SpecialDirectories.Desktop” is way to access your desktop using .Net. But the problem in that is it only supported by VB.Net.
Comment by anuraj — December 19, 2008 @ 8:22 am