Pages

Wednesday, August 25, 2010

Query Expressions Versus Method Calls

LINQ query as method calls

var bigFiles = GetAllFilesInDirectory(@"c:\").
Where(file => new FileInfo(file).Length > 10000000);
Let’s compare this with the components of the original query:
var bigFiles = from file in GetAllFilesInDirectory(@"c:\")
where new FileInfo(file).Length > 10000000
select file;


using where

var bigFiles = from file in GetAllFilesInDirectory(@"c:\")
where new FileInfo(file).Length > 10000000
select "File: " + file;


using select

var bigFiles = GetAllFilesInDirectory(@"c:\").
Where(file => new FileInfo(file).Length > 10000000).
Select(file => "File: " + file);

No comments:

Post a Comment