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

No comments: