C# - Searching for files using LINQ

By , 9/15/2008
(1 ratings)
Searching for files using LINQ

Needed Namespaces:
using System.IO;
using System.Linq;

<a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fdotnet-snippets.com%2fdns%2fsearching-for-files-using-linq-SID602.aspx"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fdotnet-snippets.com%2fdns%2fsearching-for-files-using-linq-SID602.aspx" border="0" alt="kick it on DotNetKicks.com" /></a>
Translate to VB
var files = from file in new DirectoryInfo(@"C:\").GetFiles()
            where file.Name.StartsWith("_")
            select file;

Tagged with LINQ, file, seach.


By anonymous_, 9/22/2008
This is a nice example for LINQ, but *don't* actually use it to search for files!!! This is not DLinq, which is transformed into SQL - this actually retrieves an array of _all_ the files in a directory, with all their attributes, and then performs a search in the returned array. This is *a lot* slower if the directory contains lots of files!

Log in, to comment!