private void InsertLinks(IEnumerable<string> urls)
{
EntityDataModelContext context = DataContext.GetDataContext();
foreach (string url in urls)
{
string url1 = url;
if (context.Links.Any(l => MatchUrlHash(l.UrlHash, url1)))
{
continue;
}
Link link = new Link
{
Url = url,
};
context.Links.AddObject(link);
}
context.SaveChanges();
}
private bool MatchUrlHash(long urlHash, string url)
{
SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
byte[] encoded = Encoding.ASCII.GetBytes(url);
byte[] checksum = sha.ComputeHash(encoded);
long hash = BitConverter.ToInt64(checksum, 0);
return urlHash == hash;
}
How can I convert this query to something that still performs decently and doesn’t throw any NotSupportedExceptions?
EF doesn’t know how to build SQL query with
MatchUrlHash. You can simply prepare url hash beforehand and use regular style comparison,==.