데이터베이스에서 자주 읽는 항목을 캐시하는 데 사용하려는 다음과 같은 간단한 캐시 클래스가 있습니다.동적으로 생성 된 IQueryable의 조회 키
public class Cache<TKey, TElement>
where TElement : class
{
private Dictionary<TKey, TElement> cache = new Dictionary<TKey, TElement>();
private Func<TElement, TKey> dbKey;
public Cache(Func<TElement, TKey> dbKey)
{
this.dbKey = dbKey;
}
public TElement Get(TKey key, DataContext db)
{
if (cache.ContainsKey(key)) return cache[key];
// This line throws exception. See below
var value = db.GetTable<TElement>().SingleOrDefault(x => dbKey(x).Equals(key));
if (value != null) cache[key] = value;
return value;
}
}
는 사용법은 다음과 같습니다 그러나, 표시된 라인은 메시지 "방법과 NotSupportedException이 발생
var db = new DataContext();
var userCache = new Cache<string, User>(u => u.Username);
userCache.Get("dave", db);
'으로 System.Object DynamicInvoke는 (은 System.Object는 [])'지원되는 변환이 없습니다 SQL에. " 예외가 throw되는 이유를 이해하지만 해결 방법을 모르겠습니다.
캐시는 조회 키에 다른 열이 사용되는 여러 다른 db 테이블의 앞에서 작동하도록되어 있습니다. 내 생각은 코드에서 볼 수 있듯이 람다를 전달하여 조회 열을 지정하는 것이 었습니다. IEnumerable에서는 작동하지만 IQueryable에서는 작동하지 않습니다.
LINQ to SQL에서이를 수행하는 또 다른 방법이 있습니까?