tags:

views:

226

answers:

2

Not that it would be better, but I'm trying to get my head around turning the following method syntax to query syntax to see the difference.

long diskSpace = Directory.EnumerateDirectories(@"c:\")
                    .SelectMany(Directory.EnumerateFiles)
                    .Sum(fileSize => new FileInfo(fileSize).Length);
A: 

There's no difference.

The compiler translates query syntax aka LINQ, into these methods calls. Query syntax is just syntactic suguar. It's not magic.

John Leidegren
Of course there is difference. It looks different. And that was his whole point :)
Svish
Under the hood it's the same, yes, but for coding as you can see there are big differences.
Finglas
it might be easier to, or adventagous to go with the LINQ syntax. but the fact still remains that it is identical. something you typically don't think of when writing LINQ syntax is that you pull a lot of variables from an anonymous scope into your "expression" something that, without LINQ, is not possible. LINQ syntax is, however, typically more readable than a mesh of extension methods and temporary variables.
John Leidegren
@John Aren't they both 'LINQ Syntax'?
Kirk Broadhurst
+6  A: 

That query is mostly equivalent to:

long diskSpace = (from directory in Directory.EnumerateDirectories(@"c:\")
                  from file in Directory.EnumerateFiles(directory)
                  select file)
                 .Sum(file => new FileInfo(file).Length);

(I've renamed fileSize to file to more accurately represent the meaning, btw.)

There's one actual difference in this case - we're creating a new delegate which calls Directory.EnumerateFiles rather than directly creating a delegate from the Directory.EnumerateFiles method group. In other words, it's one extra level of redirection - but this won't have any effect on the results and I'd be amazed if it had any significant performance impact.

Jon Skeet
Thanks, need to tune my mind more towards sql again, and practice the dual from syntax which would be the join.
Mikael Svenson