Wednesday, March 18, 2009

How to Use Linqs in VB.Net

With the new versions of Visual Studios, we have got the Power of LINQ(Language INtegrated Query). With the help of LINQ, we can make query on any collection like we do in SQL. What if we want to search if a string is present in an array or not. Let me explain this in an example :

I have a string Array :

Dim validXLExtns() As String = {"XLS", "XLSX", "XLA", "XLAM", "XLSM", "XLTX", "XLT", ""}

Now, I have to check programmatically if "XLS" exists in the array or not. Ideally, you will like to loop through the items in the array. But, now, with the help of LINQs you can do it with the following command :

Dim product = From s In validXLExtns _
Where s = "XLS"

And if product.count is greater than 0, the value actually exists in the array.

Some very good examples of LINQs:
MSDN LINQ Examples

No comments: