나는 다음과 같은 코드를 가지고 : 나는 where e.Id == id
절을 사용하는 경우이 간단한 동일성 비교를 어떻게 수행 할 수 있습니까?
public abstract class RepositoryBase<T, TId> : IRepository<T, TId>
where T : class, IEntityWithTypedId<TId>
where TId : IEquatable<TId>, IComparable<TId>
{
public T FindById(TId id)
{
T entity;
using (this.Context) // This simply returns the NHibernate Session
{
var entities = from e in this.Context.Get()
// where e.Id.Equals(id)
where e.Id == id
select e;
entity = entities.FirstOrDefault();
}
return entity;
}
}
, 나는 오류 얻을 :
error CS0019: Operator '==' cannot be applied to operands of type 'TId' and 'TId'
오류 나는 TId
가 구현해야하는 컴파일러를 말 했어요에도 불구을 IEquatable
및 IComparable
where e.Id.Equals(id)
절을 사용하면 코드가 컴파일되지만 NHibernate에서 쿼리를 실행할 때 NotSupported 예외가 발생합니다. FirstOrDefault
행
나는 디자인에서 뭔가를 놓치고 있어야한다는 것을 알고 있지만, 해결책은 며칠 동안 나를 벗어났다.
왜이 문제를 완전히 피하고 session.Get (id)을 사용합니까? –
Iain
엔티티 자체가 아닌 키를 비교해야합니다 ... –