I have this line of linq
_cache = _repository.EntitiesNoTracking().Select(k => Crypto.DecryptString(k.KeyValue)).ToHashSet();
This of course doesn't work, as Linq-to-entities doens't know Crypto.DecryptString
.
So the solution could be
_cache = _repository.EntitiesNoTracking().ToArray().Select(k => Crypto.DecryptString(k.KeyValue)).ToHashSet();
However, this seems to have a redundant ToArray()
in there.
I supposed the next code would be more efficient:
var _cache = new HashSet<string>();
foreach(var item in _repository.EntitiesNoTracking()){
_cache.Add(Crypto.DecryptString(item.KeyValue));
}
Isn't there some way of telling Linq-to-entities to convert to normal linq, without the extra step?
You do not need EntitiesNoTracking()
because you have custom projection. Also retrieved data should be limited by additional Select
.
_cache = _repository.
.Select(k => k.KeyValue)
.AsEnumerable()
.Select(s => Crypto.DecryptString(s))
.ToHashSet();