我正在創建例外 無法強制類型'MySomeTypeThatImplementsISomeInterfaceAndIsPassedAs [T] ToTheClass'鍵入'ISomeInterface'。 LINQ to Entities僅支持投射實體數據模型基元類型。基於接口生成的表達式
我的庫看起來像是
public interface IRepository<T>
{
IQueryable<T> Get(System.Linq.Expressions.Expression<System.Func<T, bool>> Query);
}
另外,我有服務類
public abstract class FinanceServiceBase<TAccount, TParcel, TPayment>
where TAccount : IAccount
where TParcel : IParcel
where TPayment : IPayment
{
//...
IRepository<TPayment> ParcelRepository {get; private set;}
public bool MakePayment(TPayment payment)
{
//...
ParcelRepository.Get(p => p.ParcelId == 2);
// here my exception is thrown
// **p.ParcelId is in IParcel**
//...
}
}
//...
有了這個類,我可以控制財務方面很多事情沒有其他程序重寫代碼。我已經做了類3個泛型參數,因爲我無法用IRepository,因爲我的倉庫不能<out T>
ParcelId是的Int32
TParcel是typeof運算(ParcelToReceive)是誰實施的實體IParcel,並用codeonly
產生當我打電話找並將所得拉姆達看起來像
(**(ISomeInterface)**$p).SomeInterfaceMember ==
出現的問題
而不是
($p.SomeInterfaceMember)
所以,實體框架試着做投並拋出異常。我想知道的是:無論如何告訴linq lambda字段p.ParcelId是從TParcel而不是從IParcel。
已經嘗試過(有沒有運氣):
p => ((TParcel)p).ParcelId
感謝
你的問題很混亂,措辭不當。請重新說明。 – jeroenh 2010-08-31 22:52:21
我認爲這個問題很有意義,並且已經足夠清晰。 – Timwi 2010-08-31 23:00:55