I am new to Linq and extension methods and I guess I just can't grasp the use of all the extension methods yet.
I am reading a bunch of files, and after one has been read, I'm storing the hash value of the file into a database (the file names change, and they are moved around). Periodically, I want to check a directory and read any files that I have not already, based on their hash value. So, I added an extension method to get the SHA256 hash of a file:
public static byte[] GetSHA256Hash(this FileInfo file)
{
using (FileStream fs = new FileStream(file.FullName, FileMode.Open))
{
using (SHA256 hasher = new SHA256Managed())
{
return hasher.ComputeHash(fs);
}
}
}
So, I query my database and get a list of hashes that I have already read, check the directory for all files and then I want to figure out which ones aren't in the list of files I've read. I'm positive that the byte[] in filesRead match the hashes returned by my extension method.
//Get list of hashes that have been read
List<byte[]> filesRead = GetReadFiles();
// I also need the file name, directory, etc so I can pass
// that info on to the next method that actually reads the file
var filesOnDisk = (new DirectoryInfo("c:\\dirtocheck").GetFiles("*"))
.Where(file => file.LastWriteTime > new DateTime(2009, 01, 01))
.Select(fileinfo => new { File = fileinfo, Hash = fileinfo.GetSHA256Hash() });
var filesToProcess = filesOnDisk
.Where(file => !filesRead.Contains(file.Hash))
.Select(fileinfo => fileinfo.File).ToList<FileInfo>();
My problem is that none of the files are filtered out. I've compared the hashes in filesRead to the hashes of files in filesToProcess and they match. I just don't get it.