Showing posts with label LINQs. Show all posts
Showing posts with label LINQs. Show all posts

Friday, May 15, 2009

Using Linq for Filtering File Types etc

Hi,

If you have a List of Type IO.File and you want to filter this list to contain only valid images. Then follow the following approach:

Define your array of Valid extensions :

Private IMAGES() As String = {".JPG", ".PNG", ".GIF", ".JPEG"}

The following function returns a list with Valid Images extensions as mentioned in the above array :

Public Function GetValidImage(ByVal files As List(Of File)) As List(Of File)
Dim tmpList As List(Of File) = New List(Of File)
Dim result = From s In files Group Join p In IMAGES On s.Extension.ToUpper() Equals p.ToUpper() Into Group From p In Group Select s


For Each file As File In result
tmpList.Add(file)
Next file

Return tmpList

End Function

Let me know if you have any other questions.

HTH,
Vikas

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:

Developer.com
MSDN LINQ Examples