H
Harlan Messinger
Playing with LINQ, for code to write out the number of files in a
directory as well as the total bytes in them, I got this:
string[] filePaths = System.IO.Directory.GetFiles("c:\\myfiles", "*.*",
System.IO.SearchOption.AllDirectories);
var files =
from path in filePaths
let file = new System.IO.FileInfo(path)
select file.Length;
Console.WriteLine(
"Number of files: {0:0,0}. Total size: {1:0,0} bytes.",
filePaths.Length, files.Sum());
Is there some way to return both the file count and total bytes from the
query? I was thinking of something like
select new { count = 1, size = file.Length };
but then I don't have separate sequences to apply .Sum() to. Or is there
a way to do the rollups both within the query?
directory as well as the total bytes in them, I got this:
string[] filePaths = System.IO.Directory.GetFiles("c:\\myfiles", "*.*",
System.IO.SearchOption.AllDirectories);
var files =
from path in filePaths
let file = new System.IO.FileInfo(path)
select file.Length;
Console.WriteLine(
"Number of files: {0:0,0}. Total size: {1:0,0} bytes.",
filePaths.Length, files.Sum());
Is there some way to return both the file count and total bytes from the
query? I was thinking of something like
select new { count = 1, size = file.Length };
but then I don't have separate sequences to apply .Sum() to. Or is there
a way to do the rollups both within the query?